This code generates an error:
function *giveNumbers() {
[1, 2, 3].forEach(function(item) {
yield item;
})
}
This is probabl
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.