๐Ÿ“˜yield*

the "done" value of the iterator

JS โŸฉ objects โŸฉ built-in โŸฉ Generator โŸฉ generator 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