javascript - How does Break work in for-of loop when stopping a Generator? - Stack Overflow

So there are some ways to stopping a Generator in for of loop, but how does break send a signal to the

So there are some ways to stopping a Generator in for of loop, but how does break send a signal to the Generator(in parison 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?);

So there are some ways to stopping a Generator in for of loop, but how does break send a signal to the Generator(in parison 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?);

Share Improve this question edited Feb 21, 2017 at 23:53 Mehdi Raash asked Feb 21, 2017 at 23:42 Mehdi RaashMehdi Raash 8,9752 gold badges32 silver badges43 bronze badges 6
  • You're breaking out of the for loop, what is it that you expect to happen with break? – pinkfloydx33 Commented Feb 21, 2017 at 23:45
  • shouldn't you just write "return;" to exit the loop rather than "it.return("stop");", also you've used break inside the loop which should jump out the loop anyway. – Ousmane D. Commented Feb 21, 2017 at 23:46
  • Not entirely sure what you are asking - but Break is used to break out of a loop (and continue any methods outside the loop). Return will break out of a method, not just a loop - and it can return a value. Break does not return a value. – Korgrue Commented Feb 21, 2017 at 23:47
  • Ok I'll add to question some more clearness. – Mehdi Raash Commented Feb 21, 2017 at 23:51
  • Why do you name function Generator? – guest271314 Commented Feb 21, 2017 at 23:53
 |  Show 1 more ment

2 Answers 2

Reset to default 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 pletion. 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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745652253a4638333.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信