Simple example for ES6 Generator function is misleading

RESOLVED INVALID

Status

Developer Documentation
JavaScript
RESOLVED INVALID
2 years ago
2 years ago

People

(Reporter: satbirjhuti, Unassigned)

Tracking

Details

(URL)

(Reporter)

Description

2 years ago
:: 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
```
(Reporter)

Updated

2 years ago
Keywords: dev-doc-needed, doc-bug-filed, user-doc-needed

Comment 1

2 years ago
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)
(Reporter)

Comment 2

2 years ago
Missed that conditional! This bug is invalid. Closing it.
Status: UNCONFIRMED → RESOLVED
Last Resolved: 2 years ago
Flags: needinfo?(satbirjhuti)
Resolution: --- → INVALID
Keywords: dev-doc-needed, doc-bug-filed, user-doc-needed
Priority: P4 → --
You need to log in before you can comment on or make changes to this bug.