Closed Bug 1263681 Opened 8 years ago Closed 8 years ago

Simple example for ES6 Generator function is misleading

Categories

(Developer Documentation Graveyard :: JavaScript, defect)

All
Other
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED INVALID

People

(Reporter: satbirjhuti, Unassigned)

References

()

Details

:: Developer Documentation Request

      Request Type: Correction
     Gecko Version: unspecified
 Technical Contact: 

:: Details

The comment right after the last console.log suggests the output should be `undefined`. Whereas in reality the output is `3`

Actual:
```
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // undefined
```

Expected:
```
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
```
In the example, there is a condition to stop the generator before reaching 3.
If you inspect the result of gen.next(), you should normally have the following:

gen.next(); // Object { value: 0, done: false }
gen.next(); // Object { value: 1, done: false }
gen.next(); // Object { value: 2, done: false }
gen.next(); // Object { value: undefined, done: true }

In other words, when the generator has reached its final state (when "done" is true), the "value" property doesn't matter anymore and should be undefined (according to https://tc39.github.io/ecma262/2016/#sec-iteratorresult-interface).

Do you think of a better wording for the example?
Flags: needinfo?(satbirjhuti)
Missed that conditional! This bug is invalid. Closing it.
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Flags: needinfo?(satbirjhuti)
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.