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
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers-generated.ts
Expand Up @@ -9,7 +9,7 @@ export const asyncIterator = {
minVersion: "7.15.9",
ast: () =>
template.program.ast(
'\nexport default function _asyncIterator(iterable) {\n var method;\n if (typeof Symbol !== "undefined") {\n if (Symbol.asyncIterator) method = iterable[Symbol.asyncIterator];\n if (method == null && Symbol.iterator) {\n method = iterable[Symbol.iterator];\n if (method != null) {\n return new AsyncFromSyncIterator(method.call(iterable));\n }\n }\n }\n if (method == null) method = iterable["@@asyncIterator"];\n if (method == null) {\n method = iterable["@@iterator"];\n if (method != null) return new AsyncFromSyncIterator(method.call(iterable));\n }\n if (method == null) throw new TypeError("Object is not async iterable");\n return method.call(iterable);\n}\nfunction AsyncFromSyncIterator(s) {\n AsyncFromSyncIterator = function (s) {\n this.s = s;\n this.n = s.next;\n };\n AsyncFromSyncIterator.prototype = {\n s: null,\n n: null,\n next: function () {\n return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments));\n },\n return: function (value) {\n var ret = this.s.return;\n if (ret === undefined) {\n return Promise.resolve({ value: value, done: true });\n }\n return AsyncFromSyncIteratorContinuation(ret.apply(this.s, arguments));\n },\n throw: function (value) {\n var thr = this.s.return;\n if (thr === undefined) return Promise.reject(value);\n return AsyncFromSyncIteratorContinuation(thr.apply(this.s, arguments));\n },\n };\n function AsyncFromSyncIteratorContinuation(r) {\n \n if (Object(r) !== r) {\n return Promise.reject(new TypeError(r + " is not an object."));\n }\n var done = r.done;\n return Promise.resolve(r.value).then(function (value) {\n return { value: value, done: done };\n });\n }\n return new AsyncFromSyncIterator(s);\n}\n',
'\nexport default function _asyncIterator(iterable) {\n var method, async, sync;\n if (typeof Symbol !== "undefined") {\n async = Symbol.asyncIterator;\n sync = Symbol.iterator;\n }\n do {\n if (!sync) {\n async = "@@asyncIterator";\n sync = "@@iterator";\n }\n if (async && (method = iterable[async]) != null) {\n return method.call(iterable);\n }\n if ((method = iterable[sync]) != null) {\n return new AsyncFromSyncIterator(method.call(iterable));\n }\n } while (!(sync === "@@iterator" || (sync = null)));\n throw new TypeError("Object is not async iterable");\n}\nfunction AsyncFromSyncIterator(s) {\n AsyncFromSyncIterator = function (s) {\n this.s = s;\n this.n = s.next;\n };\n AsyncFromSyncIterator.prototype = {\n s: null,\n n: null,\n next: function () {\n return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments));\n },\n return: function (value) {\n var ret = this.s.return;\n if (ret === undefined) {\n return Promise.resolve({ value: value, done: true });\n }\n return AsyncFromSyncIteratorContinuation(ret.apply(this.s, arguments));\n },\n throw: function (value) {\n var thr = this.s.return;\n if (thr === undefined) return Promise.reject(value);\n return AsyncFromSyncIteratorContinuation(thr.apply(this.s, arguments));\n },\n };\n function AsyncFromSyncIteratorContinuation(r) {\n \n if (Object(r) !== r) {\n return Promise.reject(new TypeError(r + " is not an object."));\n }\n var done = r.done;\n return Promise.resolve(r.value).then(function (value) {\n return { value: value, done: done };\n });\n }\n return new AsyncFromSyncIterator(s);\n}\n',
),
};

Expand Down
31 changes: 16 additions & 15 deletions packages/babel-helpers/src/helpers/asyncIterator.js
@@ -1,23 +1,24 @@
/* @minVersion 7.15.9 */

export default function _asyncIterator(iterable) {
var method;
var method, async, sync;
if (typeof Symbol !== "undefined") {
if (Symbol.asyncIterator) method = iterable[Symbol.asyncIterator];
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) return new AsyncFromSyncIterator(method.call(iterable));
async = Symbol.asyncIterator;
sync = Symbol.iterator;
}
if (method == null) throw new TypeError("Object is not async iterable");
return method.call(iterable);
do {
if (!sync) {
async = "@@asyncIterator";
sync = "@@iterator";
}
if (async && (method = iterable[async]) != null) {
return method.call(iterable);
}
if ((method = iterable[sync]) != null) {
return new AsyncFromSyncIterator(method.call(iterable));
}
} while (!(sync === "@@iterator" || (sync = null)));
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
throw new TypeError("Object is not async iterable");
}

function AsyncFromSyncIterator(s) {
Expand Down