【发布时间】:2016-08-19 19:35:07
【问题描述】:
背景
我正在试验Generator.prototype.throw() 的工作原理并制作了这个例子:
var myGen = function *() {
try{
yield 1;
yield 2;
yield 3;
yield 4;
yield 5;
}
catch(err) {
console.log(err);
}
yield 7;
yield 8;
yield 9;
}
var myIterator = myGen();
console.log(myIterator.next());
console.log(myIterator.next());
console.log(myIterator.next());
myIterator.throw(new Error('Bullocks!'));
console.log(myIterator.next());
console.log(myIterator.next());
console.log(myIterator.next());
在运行时会产生以下结果:
{ value: 1, done: false }
{ value: 2, done: false }
{ value: 3, done: false }
[Error: Bullocks!]
{ value: 8, done: false }
{ value: 9, done: false }
{ value: undefined, done: true }
问题
我可以理解yield 4和try块的剩余部分在抛出错误后被跳过。
但是为什么生成器会跳过yield 7?
【问题讨论】:
标签: javascript ecmascript-6 generator