Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override the next method of an asynchronous iterator doesn't work in v10 #21125

Closed
celicoo opened this issue Jun 4, 2018 · 5 comments
Closed
Labels
question Issues that look for answers.

Comments

@celicoo
Copy link

celicoo commented Jun 4, 2018

Bug Report

Current Behavior
Override the next method of an asynchronous iterator object doesn't work in v10.

Input Code

const customIterator = {
  [Symbol.asyncIterator]() {
    return {
      next: this.next
    }
  },

  next() {
    console.log('outter')
    this.next = () => {
      console.log('inner')
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve({
            done: false,
            value: {}
          })
        }, 1000)
      })
    }
    return this.next()
  }
}

async function test() {
  for await (const x of customIterator) {}
}
test()

Expected behavior/code
The output should be outter printed, followed by multiple inner, but in fact, the looping always prints outter and then inner, repeatedly.

Expected:

outter
inner
inner
inner
inner
...

Reality:

outter
inner
outter
inner
outter
inner

Environment

  • Node/npm version: 10.3/ 5
  • OS: 10.13.4
@celicoo
Copy link
Author

celicoo commented Jun 5, 2018

I realized that Chrome and Firefox have the same behaviour of Node 10. Does that mean that the wrong behaviour happens on Node 8? (behind --harmony_async_iteration), and more important - Is it a internal cache of await for...of?

@mscdex
Copy link
Contributor

mscdex commented Jun 5, 2018

Any features like that that are only available behind a flag should be considered experimental. My guess is that what you're experiencing now is the expected behavior.

@celicoo
Copy link
Author

celicoo commented Jun 5, 2018

I'm affraid that's the case, which makes me extremely disappointed since method overriding is something that should be possible in this case.

The only way to achieve the same result that I can think of is managing state with a control variable.

@CertainPerformance
Copy link

See this StackOverflow question - it doesn't look to be a bug in Node. The same behavior is visible in native browser-based JS as well.

@bnoordhuis bnoordhuis added the question Issues that look for answers. label Jun 5, 2018
@bnoordhuis
Copy link
Member

Answered, closing. Next time, please post to nodejs/help if you're not sure it's a bug in Node.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

4 participants