使用如下所示的迭代器:
function* idMaker(){
let index = 0;
while(index < 3)
yield index++;
}
如何确保根据情况提前停止发电机?
function* idMaker(){
let index = 0;
while(index < 3)
if (checker(index))
yield index++;
else
return;
}
适合在发电机中使用吗?我应该使用break
代替yield break
也许就像在 C# 中一样?
您能否提供一个 MDN 链接或讨论此问题的规范?
你有没有尝试过看看return
生成器的方法?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/return https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/return
function* gen() {
yield 1;
yield 2;
yield 3;
}
const g = gen();
g.next(); // { value: 1, done: false }
g.return('foo'); // { value: "foo", done: true }
g.next(); // { value: undefined, done: true }
g.return('bar'); // { value: "bar", done: true }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)