Sub topic 14
break, continue and Early Exit
This chapter assumes you know for...of syntax and the iterator protocol from the previous chapter: for...of gets an iterator from Symbol.iterator and calls next() until done is true. It also assumes basic familiarity with generator functions.
How each statement behaves
break, continue and return work in for...of the way they do in a classic for loop. One difference matters, though. When the loop stops before the iterator reports done: true, JavaScript calls the iterator's return() method, if it has one. This step is called closing the iterator, and it lets the source release resources such as file handles, timers or locks.
| Statement | Effect on the loop | Calls `iterator.return()`? |
|---|---|---|
| `continue` | Skips to the next `next()` call | No |
| `break` | Leaves the loop | Yes |
| `return` (inside a function) | Leaves the loop and the function | Yes |
| An uncaught `throw` | Leaves the loop with an error | Yes |
| Normal completion | Ends after `done: true` | No, the iterator is already finished |
Watching cleanup happen
A generator's finally block runs when its iterator is closed, so it shows when closing happens:
function* readLines() {
try {
yield 'header';
yield 'row 1';
yield 'row 2';
yield 'row 3';
} finally {
console.log('closing source');
}
}
function findFirstRow() {
for (const line of readLines()) {
if (line === 'header') continue; // no cleanup, loop keeps going
return line; // early exit: cleanup runs
}
}
console.log(findFirstRow());
// closing source
// row 1continue skips 'header' and asks for the next value. return exits before the generator is finished, so JavaScript closes it and 'closing source' prints before the caller logs the result.
A closed generator stays closed
Closing is permanent. If you break out of a loop over a generator object and then loop over the same object again, the second loop gets nothing:
function* nums() {
yield 1;
yield 2;
yield 3;
}
const gen = nums();
for (const n of gen) {
console.log(n);
break;
}
// 1
for (const n of gen) {
console.log(n);
}
// (prints nothing)Arrays behave differently. Each for...of over an array calls Symbol.iterator again and gets a new iterator, so breaking out of one loop does not affect the next. Array iterators also have no return() method, so they have nothing to clean up.
Labels for nested loops
A bare break only leaves the innermost loop. To leave an outer loop, put a label on it. Each iterator that gets exited is closed:
const grid = [[1, 2], [3, 4], [5, 6]];
outer: for (const row of grid) {
for (const cell of row) {
if (cell === 4) break outer;
console.log(cell);
}
}
// 1
// 2
// 3Exercise
A generator yields 'a', 'b' and 'c' inside try { ... } finally { console.log('done'); }. A for...of loop over it runs continue on 'a' and logs the other values. Is 'done' printed, and when? What changes if the loop runs break on 'b'?
Show answer
With continue, the loop runs to the end and logs 'b' and 'c'. After that, the generator reaches the end of its try block, so 'done' prints once, as the last line. No return() call is involved. With break on 'b', the loop calls return() on the generator while it is paused at the yield of 'b'. The finally block runs and 'done' prints right away. 'c' is never produced, so only 'done' is logged.