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

Await promises from sync iterators with for-await #13824

Merged
merged 4 commits into from Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 45 additions & 3 deletions packages/babel-helpers/src/helpers.ts
Expand Up @@ -16,18 +16,60 @@ const helper = (minVersion: string) => (tpl: TemplateStringsArray) => ({
ast: () => template.program.ast(tpl),
});

helpers.asyncIterator = helper("7.0.0-beta.0")`
helpers.asyncIterator = helper("7.15.9")`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: why should we bump helper minimum version here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh probably it's not needed. It's always a choice between "updating the plugin and @babel/core is enough to get the bugfix" vs "you also need to update @babel/runtime".

export default function _asyncIterator(iterable) {
var method;
if (typeof Symbol !== "undefined") {
if (Symbol.asyncIterator) method = iterable[Symbol.asyncIterator];
if (method == null && Symbol.iterator) method = iterable[Symbol.iterator];
if (method == null && Symbol.iterator) {
method = iterable[Symbol.iterator];
if (method != null) return new AsyncFromSyncIterator(method.call(iterable));
}
}
if (method == null) method = iterable["@@asyncIterator"];
if (method == null) method = iterable["@@iterator"]
if (method == null) {
method = iterable["@@iterator"];
if (method != null) return new AsyncFromSyncIterator(method.call(iterable));
}
if (method == null) throw new TypeError("Object is not async iterable");
return method.call(iterable);
}

function AsyncFromSyncIterator(s) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AsyncFromSyncIterator = function (s) {
this.s = s;
this.n = s.next;
};
AsyncFromSyncIterator.prototype = {
/* SyncIterator */ s: null,
/* SyncIterator.[[Next]] */ n: null,
next: function () {
return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments));
},
return: function (value) {
var ret = this.s.return;
if (ret === undefined) return Promise.resolve({ value: value, done: true });
return AsyncFromSyncIteratorContinuation(ret.apply(this.s, arguments));
},
throw: function (value) {
var thr = this.s.return;
if (thr === undefined) return Promise.reject(value);
return AsyncFromSyncIteratorContinuation(thr.apply(this.s, arguments));
},
};

function AsyncFromSyncIteratorContinuation(r) {
// This step is _before_ calling AsyncFromSyncIteratorContinuation in the spec.
if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object."));

var done = r.done;
return Promise.resolve(r.value).then(function (value) {
return { value: value, done: done };
});
}

return new AsyncFromSyncIterator(s);
}
`;

helpers.AwaitValue = helper("7.0.0-beta.0")`
Expand Down
@@ -0,0 +1,12 @@
async function* fn() {
for await (const result of [Promise.resolve("ok")]) {
return { result };
}
}

return fn().next().then(result => {
expect(result).toEqual({
done: true,
value: { result: "ok" }
});
});
@@ -0,0 +1,5 @@
async function* fn() {
for await (const result of [Promise.resolve("ok")]) {
return { result };
}
}
@@ -0,0 +1,6 @@
{
"plugins": ["proposal-async-generator-functions"],
"parserOpts": {
"allowReturnOutsideFunction": true
}
}
@@ -0,0 +1,35 @@
function fn() {
return _fn.apply(this, arguments);
}

function _fn() {
_fn = babelHelpers.wrapAsyncGenerator(function* () {
var _iteratorAbruptCompletion = false;
var _didIteratorError = false;

var _iteratorError;

try {
for (var _iterator = babelHelpers.asyncIterator([Promise.resolve("ok")]), _step; _iteratorAbruptCompletion = !(_step = yield babelHelpers.awaitAsyncGenerator(_iterator.next())).done; _iteratorAbruptCompletion = false) {
const result = _step.value;
return {
result
};
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (_iteratorAbruptCompletion && _iterator.return != null) {
yield babelHelpers.awaitAsyncGenerator(_iterator.return());
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
});
return _fn.apply(this, arguments);
}
@@ -0,0 +1,7 @@
async function* fn() {
yield* [Promise.resolve("ok")] // CreateAsyncFromSyncIterator
}

return fn().next().then(result => {
expect(result).toEqual({ value: "ok", done: false });
});
@@ -0,0 +1,3 @@
async function* fn() {
yield* [Promise.resolve("ok")] // CreateAsyncFromSyncIterator
}
@@ -0,0 +1,6 @@
{
"plugins": ["proposal-async-generator-functions"],
"parserOpts": {
"allowReturnOutsideFunction": true
}
}
@@ -0,0 +1,10 @@
function fn() {
return _fn.apply(this, arguments);
}

function _fn() {
_fn = babelHelpers.wrapAsyncGenerator(function* () {
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator([Promise.resolve("ok")]), babelHelpers.awaitAsyncGenerator); // CreateAsyncFromSyncIterator
});
return _fn.apply(this, arguments);
}