5

So there are some ways to stopping a Generator in for of loop, but how does break send a signal to the Generator(in comparison with return in for-of)?

please consider the code.

As an example, the preceding code just increases a value from 1 to 10 ,and do pause and resume in between.

function *Generator() {

    try {
        var nextValue;

        while (true) {
            if (nextValue === undefined) {
                nextValue = 1;
            }
            else {
                nextValue++;
            }

            yield nextValue;
        }
    }
    // cleanup clause
    finally {
        console.log( "finally has been reached." );
    }
}

it loops over it 10 times by using for of:

var it = Generator();// it gets Generator's iterator

for (var v of it) {
    console.log(v); 

    if (v > 9) {

           //it.return("stop");//this is another tool for stopping, but it doesn't stop immediately.

           break; 

           console.log("it won't run");//this line won't run
    }
} 

When it.return() is used by the way, the implementation's clear(it is the main Object and has got the control, but what about the break?);

Mehdi Raash
  • 8,721
  • 2
  • 29
  • 42

2 Answers2

4

Iterable objects like your it generator object have a property with the key Symbol.iterator that is a function returning an iterator. Iterators are required to have a .next() method to advance from one item to the next. Then can also optionally have a .return() method, which is called when you break, return, or throw, causing the for..of to stop before it runs to completion. So in your case, break; will automatically call it.return().

The other side of it is that on ES6 generator, .next() makes it resume execution at the currently paused yield, and .return() makes it act like the yield is a return statement, so break inside the loop causes yield nextValue; to behave like return;, which will exit the generator and trigger the finally block.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • I appreciate it, You said in my case `break` will automatically call `it.return()`, but it just put `{done: true}` which means we haven't actually break the `for-of` loop body, we just told our iterator that it's done. So `break` after calling `it.return()` terminates its own loop iteration? (it does two jobs right?) – Mehdi Raash Feb 22 '17 at 00:38
  • 3
    @MehdiRaash What do you mean by "*but it just put `{done: true}`*"? No, `break` breaks out of the loop like it always does, and then the `for` will call `it.return()` before continuing with the next statement after the loop. – Bergi Feb 22 '17 at 00:54
  • Yeah, you never call `it.return()` yourself with `for..of`, just like you never call `it.next()` yourself. Putting a `break` in your loop will call `.return` for you. – loganfsmyth Feb 22 '17 at 01:14
2

how does break send a signal to the Generator?

The loop will call the IteratorClose operation, which basically amounts to invoking the iterator's .return() method with no arguments if the iterator object has such a method - which generators do. This also happens when a throw or return statement in the loop body is evaluated.

When it.return() is used by the way, the implementation is clear

…but horrible. As you found out, it doesn't stop immediately. That's because a method call just advances the generator and gets you some result back from it, but is has nothing to do with your loop. The loop body will just continue to be executed until the loop condition is evaluated again, which then will check the iterator and notice that it's already done.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375