That First Next Argument

Claude Pache claude.pache at
Mon Aug 18 23:48:30 PDT 2014

Le 19 août 2014 à 06:47, Kevin Smith <zenparsing at> a écrit :

> Background:
> It appears that the current state of affairs is that the argument supplied to the first call of `next` on a newborn generator is "ignored and inaccessibe".
> Clearly, this means that there are some iterators which cannot be expressed as a generator (namely, any iterator that echoes back it's `next` arguments).  It seems like there should be parity here.
> More concretely, the fact that information can be passed into generators means that they can be used to create data sinks.  Since that first input is inaccessible, however, this use case is made more awkward than it needs to be; the consumer has to artificially "pump" the generator to get past that first (useless) `next`.
> Is there any way that the generator function can have access to that lost data?
> Thanks!

This can be worked around. Basically, ask the generator to advance to the first `yield` at instantiation, and retrieve the value of the "first" `next()` with that `yield`. For example:

// the echo generator, dropping the first .next()
function* echo() {
    var x
    while (true) {
        x = yield x

var iter = echo() // {value: undefined, done: false} // {value: 8, done: false} // {value: 1, done: false}

// the same, advancing to the first `yield` at instantiation
class echo2 extends echo {
    construct(...args) {
        let iter = super(...args)
        return iter

var iter = echo2() // {value: 3, done: false} // {value: 8, done: false} // {value: 1, done: false}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list