简体   繁体   English

在 ES6 生成器函数中使用 return

[英]Using return in ES6 generator function

I am having trouble finding out what happens if you use a return statement instead of yield.如果您使用 return 语句而不是 yield,我很难弄清楚会发生什么。

function *gen(){

 const val = yield someAsyncFn();
 assert.equal(val,4);
 return val;

}

how does the return behave differently from the yield?回报与收益率有何不同? I assume the return acts as a normal return statement, but the context of a generator function, does it also call gen.return() as well?我假设 return 充当正常的 return 语句,但是生成器函数的上下文是否也调用gen.return() Sort of confusing.有点混乱。

Perhaps the above is merely identical to this?也许以上仅与此相同?

   function *gen(){

     const val = yield someAsyncFn();
     assert.equal(val,4);
     yield val;

   }

return deliveres a return value for an iterators last iteration (when done equals true ). return为迭代器最后一次迭代传递返回值(当done等于true时)。

I've simplified your example a bit, since the async operation doesn't seem to be relevant to the question:我稍微简化了您的示例,因为异步操作似乎与问题无关:

function *gen(){
 const val = yield 4;
 return val * 2;
}

var it = gen();
var val = it.next(); // { value: 4, done: false }
console.log(val.value); // 4
var res = it.next(val.value); // { value: 8, done: true }
console.log(res.value); // 8

Whereas without a return value, on the last iteration you will return a value of undefined :虽然没有return值,但在最后一次迭代中您将返回一个值undefined

function *gen2(){
 const val = yield 4;
 yield val * 2;
}

var it2 = gen2();
var val2 = it2.next(); // { value: 4, done: false }
console.log(val2.value); // 4
var res2 = it2.next(val2.value); // { value: 8, done: false }
console.log(res2.value); // 8
it2.next(); // { value: undefined, done: true }

Sidenote: As a rule of thumb, there is always one more next call then there are yield statements, which is why there is one more next call in the second example.旁注:根据经验,在yield语句之后总会有一个next调用,这就是第二个示例中还有一个下一个调用的原因。

Let's say you're using a generator-runner like co , then the value you get after finishing the generator would be the value you return :假设您正在使用像co这样的生成器运行程序,那么完成生成器后获得的值将是您return的值:

co(function* () {
  var result = yield Promise.resolve(true);
  return result;
}).then(function (value) {
  console.log(value); // value equals result
}, function (err) {
  console.error(err.stack);  // err equals result
});

Important: If you are iterating through an iterator, using a for... of loop or something like Array.from , the return value is going to be ignored (Since you are doing async operations, this is probably not the case anyway):重要提示:如果您正在迭代迭代器,使用for... of循环或类似Array.from的东西,则return值将被忽略(因为您正在进行异步操作,所以情况可能并非如此):

function *gen(){
 const val = yield 4;
 return val * 2;
}

for (let value of gen()) {
  console.log(value);
}
// 4

In the end, calling a generator just creates an iterator.最后,调用生成器只会创建一个迭代器。 Whether the final value that the iterator returns is relevant, depends entirely on how you use it.迭代器返回的最终值是否相关,完全取决于您如何使用它。

In addition to the thorough answer by @nils, there is one additional way to capture the return value of a generator function, namely as the value of yield* (necessarily inside another generator function):除了@nils 的详尽回答之外,还有一种捕获生成器函数返回值的方法,即作为yield*的值(必须在另一个生成器函数中):

  function* arrayGenerator(arr) {
    for (const element of arr) 
      yield element
    return arr.length
  }

  function* elementsFollowedByLength(arr) {
    const len = yield* arrayGenerator(arr);
    yield len;
  }

Note the first generator function which returns a value, after it is done yielding the array elements.请注意第一个返回值的生成器函数,它完成生成数组元素后。

The second generator function, through yield* , causes the first generator function to yield all its values.第二个生成器函数通过yield*使第一个生成器函数产生它的所有值。 When the first generator function is done and returns, that return value becomes the value of the yield* expression.当第一个生成器函数完成并返回时,该返回值成为yield*表达式的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM