We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
完整代码
var _marked = regeneratorRuntime.mark(helloWorldGenerator); function helloWorldGenerator() { // 1. wrap 之后返回一个 generator 具有原型上继承有 next 方法 return regeneratorRuntime.wrap( function helloWorldGenerator$(_context) { while (1) { switch ((_context.prev = _context.next)) { case 0: _context.next = 2; return "hello"; case 2: _context.next = 4; return "world"; case 4: return _context.abrupt("return", "ending"); case 5: case "end": // 8. 最后一次直接执行 stop 没有参数传入,value 为 undefined return _context.stop(); } } }, _marked, this ); } // function* helloWorldGenerator() { // yield 'hello'; // yield 'world'; // return 'ending'; // } var hw = helloWorldGenerator(); console.log(hw.next()); console.log(hw.next()); console.log(hw.next()); console.log(hw.next());
调用的时候关键的是通过 wrap 函数返回一个 函数,进入 wrap 函数查看,
function wrap(innerFn, outerFn, self) { var generator = Object.create(outerFn.prototype); var context = { done: false, method: "next", next: 0, prev: 0, // 6. 'ending' 作为 arg 传入 abrupt: function (type, arg) { var record = {}; record.type = type; record.arg = arg; return this.complete(record); }, complete: function (record, afterLoc) { if (record.type === "return") { this.rval = this.arg = record.arg; this.method = "return"; this.next = "end"; } return ContinueSentinel; }, stop: function () { this.done = true; // 7. 第三次的 next 最终返回值在此处,返回给 70 行 record arg return this.rval; } }; generator._invoke = makeInvokeMethod(innerFn, context); return generator; }
wrap 函数返回了一个 generator 函数,该函数有继承自 outerFn 的 next 方法。这也是 next 能被执行的原因。
var mark = function (genFun) { var generator = Object.create({ next: function (arg) { // 关键的地方 // 2. this 指向调用对象,即 wrap 中的 generator return this._invoke("next", arg); } }); genFun.prototype = generator; return genFun; };
经过分析,最后执行的是 function invoke 。invoke 函数其中的一个 value 结果来自 Babel 编译出的 helloWorldGenerator$ 函数调用后的返回结果。
The text was updated successfully, but these errors were encountered:
Sorry, something went wrong.
No branches or pull requests
完整代码
调用的时候关键的是通过 wrap 函数返回一个 函数,进入 wrap 函数查看,
wrap 函数返回了一个 generator 函数,该函数有继承自 outerFn 的 next 方法。这也是 next 能被执行的原因。
经过分析,最后执行的是 function invoke 。invoke 函数其中的一个 value 结果来自 Babel 编译出的 helloWorldGenerator$ 函数调用后的返回结果。
The text was updated successfully, but these errors were encountered: