Nested yield and yield*

We can nest yield and yield* to create complex values generation.

Nested yield

In the code below, the inner yield expression generates value 1 first, then the middle yield expression generates value of yield 1 - undefined, then the outer yield expression generates value of yield yield 1 - undefined.

function *manyYields() {
  yield yield yield 1;
}

debug(manyYields());
// Output 1, undefined, undefined

Nested yield and yield*

In the code below, generator oneToThree first generates three values 1, 2 and 3, then its value undefined is generated by yield expression.

function *oneToThree() {
  yield* [1, 2, 3];
}

function *values() {
  yield yield* oneToThree();
}

debug(values());
// -> Output 1, 2, 3, undefined

results matching ""

    No results matching ""