Javascript yield from the function nested inside generator

后端 未结 4 847
醉酒成梦
醉酒成梦 2021-01-03 23:55

This code generates an error:

function *giveNumbers() {
    [1, 2, 3].forEach(function(item) {
        yield item;
    })
}

This is probabl

相关标签:
4条回答
  • 2021-01-04 00:22

    You can also use while and pass arguments as such (Demo)

    function *giveNumbers(array, start) {
        var index = start || 0;
        while(array.length > index + 1) {
            yield array[index++];
        }
        return array[index];
    }
    
    
    var g = giveNumbers([1,2,3], 0);
    
    var finished = false;
    
    while(!finished) {
        var next = g.next();
        console.log(next.value);
        if(next.done) {
            finished = true;
        }
    }
    
    0 讨论(0)
  • 2021-01-04 00:25

    you can use the yield * syntax.

    function *giveNumbers() {
        yield * [1, 2, 3].map(function(item) {
            return item;
        })
    }
    
    0 讨论(0)
  • 2021-01-04 00:34

    This is probably because yield is inside a function that is not a generator.

    Yes. You cannot use yield from callbacks.

    Is there an elegant way to overcome this?

    Depends on the use case. Usually there is zero reason to actually want to yield from a callback.

    In your case, you want a for…of loop, which is superior to .forEach in almost every aspect anyway:

    function *giveNumbers() {
        for (let item of [1, 2, 3])
            yield item;
    }
    
    0 讨论(0)
  • 2021-01-04 00:38

    yield returns the result to the caller.
    let's assume the forEach callback is a generator (it's not a problem to set a costume generator there) - it means tha when the callback yield the result - it yields it back to forEach.

    Basically, in your question what you attemp to do is:

    callback -> yields to forEach -> yields to giveNumbers -> yields to caller
    

    So, forEach should yield the result back to giveNumbers. but since forEach doesn't work like this, it's impossible without re-prototype arrays with costume forEach.

    Actually, you second snippet is the most elegant to begin with.

    0 讨论(0)
提交回复
热议问题