The following example shows a simple generator and the return method.
function* gen() {
yield 1;
yield 2;
yield 3;
}
const g = gen();
g.next();
g.return("foo");
g.next();
If return(value) is called on a generator that is already in "completed" state, the generator will remain in "completed" state.
If no argument is provided, the value property of the returned object will be undefined. If an argument is provided, it will become the value of the value property of the returned object, unless the yield expression is wrapped in a try...finally.
function* gen() {
yield 1;
yield 2;
yield 3;
}
const g = gen();
g.next();
g.next();
g.next();
g.next();
g.return();
g.return(1);