admin管理员组文章数量:1357294
I know the use of next() method which advance the iterator to the next position of yield keyword and return the object which contains the result of yield expression, like this:
yield double(40); => call iterator.next() would return something like {done:false, value:80}
However I'm not very clear if the throw() method would also advance the iterator and return the next yield's expression, besides throwing an exception into the generator. The documentation says like this:
The throw() method resumes the execution of a generator by throwing an error into it and returns an object with two properties done and value.
When we call iterator.throw(), will it throw the exception into the generator (the exception is caught correctly), and advance to the next yield keyword and return the result of the yield expression?
Please let me know if I need to include code snippet to demonstrate this, as this is my first post on Stackoverflow.
Thanks!!!
I know the use of next() method which advance the iterator to the next position of yield keyword and return the object which contains the result of yield expression, like this:
yield double(40); => call iterator.next() would return something like {done:false, value:80}
However I'm not very clear if the throw() method would also advance the iterator and return the next yield's expression, besides throwing an exception into the generator. The documentation says like this:
The throw() method resumes the execution of a generator by throwing an error into it and returns an object with two properties done and value.
When we call iterator.throw(), will it throw the exception into the generator (the exception is caught correctly), and advance to the next yield keyword and return the result of the yield expression?
Please let me know if I need to include code snippet to demonstrate this, as this is my first post on Stackoverflow.
Thanks!!!
Share Improve this question asked Jun 7, 2016 at 8:32 Phat NguyenPhat Nguyen 631 silver badge5 bronze badges1 Answer
Reset to default 10It does advance the generator from the yield which received the error. However, since the throw
induced an exception at the point of the yield, unless the generator code is prepared to catch the exception and swallow it, the generator will not resume and will plete (done is set to true), bubbling up the uncaught exception to the iterator call site.
Here's a simple demo, notice the genDrunk throwing up ;) -
function* genSober() {
while(true) {
try {
yield 42;
} catch(e) {
console.log("I don't drink at work!");
}
}
}
var g = genSober();
console.log(g.next());
// { value: 42, done: false }
g.throw("Have some wine...");
console.log(g.next());
function* genDrunk() {
while(true) {
yield 42;
}
}
g = genDrunk();
console.log(g.next());
try {
g.throw("Have some wine...");
}
catch (e){
}
console.log(g.next());
本文标签: Javascript Generator throw() method advance the iteratorStack Overflow
版权声明:本文标题:Javascript Generator throw() method advance the iterator? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744074840a2586552.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论