return method

A generator object has a return method to return given value and finish the generator. This behavior is similar with using return statement inside of a generator.

Given the same values generator function shown below,

function *values() {
  yield 'a';
  yield 'b';
  yield 'c';

We can see how invoking return method finishes the generator object. The first next() invocation returns the first value 'a', then func.return('d') returns value 'd' and finishes the generator, i.e. done property is set to true.

let func = values();;
// -> {value: "a", done: false}
// -> {value: "d", done: true};
// -> {value: undefined, done: true}

return method can be invoked multiple times. Each invocation returns the value passed to return() method.

let func = values();;
// -> {value: "a", done: false};
// -> {value: "b", done: false};
// -> {value: "c", done: false};
// -> {value: undefined, done: true}
// -> {value: "d", done: true}
// -> {value: "e", done: true}

results matching ""

    No results matching ""