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 badges
Add a ment  | 

1 Answer 1

Reset to default 10

It 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