首页 > 解决方案 > ES6 中的生成器

问题描述

我有代码:

function countA() {
  return 1;
}

function* sendStuff() {
  let y = yield countA();
  console.log("y: ", y);
  yield;
}

var gen = sendStuff();

console.log(gen.next().value);
console.log(gen.next().value);

结果是:

    > 1
    > "y: " undefined
    > undefined

为什么 y 未定义?我知道我应该是 1(从 countA 结果中获取)。请帮帮我。十分感谢

标签: ecmascript-6generator

解决方案


推荐阅读