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

Improve asyncIterator error #7745

Merged
merged 4 commits into from Apr 18, 2018

Conversation

@jquense
Copy link
Contributor

jquense commented Apr 17, 2018

When an object is has neither asyncIterator or iterator defined, throw the "not an async iterable" error

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR
Any Dependency Changes?
License MIT
When an object is has neither asyncIterator or iterator defined, throw the "not an async iterable" error
Copy link
Member

nicolo-ribaudo left a comment

The helper should first look for iterable[Symbol.asyncIterator], the for iterable[Symbol.iterator].

This patch, when Symbol.asyncIterator exists, doesn't look for iterable[Symbol.iterator].

@babel-bot

This comment has been minimized.

Copy link
Collaborator

babel-bot commented Apr 17, 2018

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/7621/

@jquense

This comment has been minimized.

Copy link
Contributor Author

jquense commented Apr 17, 2018

var method = iterable[Symbol.asyncIterator];
if (method != null) return method.call(iterable);
if (Symbol.asyncIterator && iterable[Symbol.asyncIterator] != null) {
return iterable[Symbol.asyncIterator]();

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Apr 17, 2018

Member

After reading this again, I noticed a little bug: it gets iterable[Symbol.asyncIterator] and iterable[Symbol.iterator] twice.
It should be something like this:

if (Symbol.asyncIterator && (method = iterable[Symbol.asyncIterator]) != null) return method.call(iterable);

This comment has been minimized.

Copy link
@jquense

jquense Apr 17, 2018

Author Contributor

whats wrong with accessing it twice?

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Apr 17, 2018

Member
var i = 0;

var obj = {
  get [Symbol.asyncIterator]() {
    i++;
    return async function* () { yield 1; yield 2; };
  }
};

(async () => {
  for await (const x of obj) {}
  alert(i); // Should alert 1, not 2
})();

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Apr 17, 2018

Member

^ Actually this should be a test, but it can be added in a different PR.

This comment has been minimized.

Copy link
@jquense

jquense Apr 17, 2018

Author Contributor

that seems like a bit of a contrived example, but sure

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Apr 17, 2018

Member

Usually if it doesn't add too much complexity and it isn't slower, we try to be as much spec compliant as possible. Also, it looks like this case was specifically handled in the old helper.

@nicolo-ribaudo nicolo-ribaudo dismissed their stale review Apr 17, 2018

Not yet ready

jquense added 2 commits Apr 17, 2018
@Jessidhia Jessidhia modified the milestone: Babel 7 RC Apr 18, 2018
@existentialism existentialism merged commit 2bded40 into babel:master Apr 18, 2018
4 checks passed
4 checks passed
babel/repl REPL preview is available
Details
ci/circleci Your tests passed on CircleCI!
Details
codecov/project 80.85% (target 80%)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@lock lock bot added the outdated label Oct 5, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants
You can’t perform that action at this time.