Skip to content

Commit

Permalink
Clarify usage of return values in iterator protocol (mdn#37511)
Browse files Browse the repository at this point in the history
* Clarify usage of return values in iterator protocol

There's an example with an iterable that returns an iterator that enumerates 1, 2, and returns with `{done: true, value: 3}`, and is evaluated with `const [b, c, d] = obj`.

It takes a careful reading to notice that the third value is used only to ensure that the iterator is exhausted and that the value from the iterator return is not bound to `d`.

* Update files/en-us/web/javascript/reference/iteration_protocols/index.md

---------

Co-authored-by: Joshua Chen <[email protected]>
  • Loading branch information
etrepum and Josh-Cena authored Jan 6, 2025
1 parent 0e5b7e1 commit 21916b1
Showing 1 changed file with 1 addition and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ const [b, c, d] = obj;
// Returning 3
// Already reached the end (the last call returned `done: true`),
// so `return` is not called
console.log([b, c, d]); // [1, 2, undefined]; the value associated with `done: true` is not reachable

for (const b of obj) {
break;
Expand Down

0 comments on commit 21916b1

Please sign in to comment.