📘yield*

the "done" value of the iterator

JSobjectsbuilt-inGeneratorgenerator function ⟩ yield* expression

the value of yield* expression is the "done" value of the iterator. (undefined by default)

const { log } = console;

// generator: a()
function* a() {
    yield* [1, 2, 3];
    return 'foo';                // ⭐️ "done" value of `a`
}

// generator: b()
function* b() {
    
    // --------------------------------------------------------------
    const value = yield* a();    // ⭐️ "done" value of `a` returned
                                 //     after it's finished yielding.
    // --------------------------------------------------------------
    
    console.log(value)           // console: 'foo'
    
    return value + value;        // ⭐️ "done" value of `b`
}

// (iterable) iterators
const it1 = a();

log(it1.next());    // {value: 1, done: false}
log(it1.next());    // {value: 2, done: false}
log(it1.next());    // {value: 3, done: false}
log(it1.next());    // {value: 'foo'⭐️, done: true}

log(it1.next());    // {value: undefined❗, done: true}
                    // ⭐️ "done" value is only used once❗

const it2 = b();

log(it2.next());    // {value: 1, done: false}
log(it2.next());    // {value: 2, done: false}
log(it2.next());    // {value: 3, done: false}

log(it2.next());    // console: 'foo' <--- ⭐️ "done" value of `a` returned here
                    // {value: 'foofoo'⭐️, done: true}

Last updated