February 2018
Intermediate to advanced
298 pages
8h 22m
English
You can end a generator function any time before it has yielded all the values by using the return() method of the generator object. The return() method takes an optional argument, representing the final value to return.
Here is an example demonstrating this:
function* generator_function(){ yield 1; yield 2; yield 3;}const generator = generator_function();console.log(generator.next().value);console.log(generator.return(22).value);console.log(generator.next().done);
The output is as follows:
122true
Read now
Unlock full access