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
nicolo-ribaudo
merged 4 commits into
babel:main
from
nicolo-ribaudo:async-from-sync-iterator
Oct 7, 2021
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3942b46
Await promises from sync iterators with `for-await`
nicolo-ribaudo eb16405
Extract helper to its own file
nicolo-ribaudo ceb9fec
Optimize helper size (934 vs 881 bytes minified)
nicolo-ribaudo 34b58f0
Optimize helper size (881 vs 855 bytes minified)
nicolo-ribaudo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")` | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The spec for this is at https://tc39.es/ecma262/#sec-async-from-sync-iterator-objects |
||
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")` | ||
|
12 changes: 12 additions & 0 deletions
12
...async-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
...sync-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
async function* fn() { | ||
for await (const result of [Promise.resolve("ok")]) { | ||
return { result }; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": ["proposal-async-generator-functions"], | ||
"parserOpts": { | ||
"allowReturnOutsideFunction": true | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ync-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
7 changes: 7 additions & 0 deletions
7
...sync-generator-functions/test/fixtures/yield-star/create-async-from-sync-iterator/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
async function* fn() { | ||
yield* [Promise.resolve("ok")] // CreateAsyncFromSyncIterator | ||
} | ||
|
||
return fn().next().then(result => { | ||
expect(result).toEqual({ value: "ok", done: false }); | ||
}); |
3 changes: 3 additions & 0 deletions
3
...ync-generator-functions/test/fixtures/yield-star/create-async-from-sync-iterator/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
async function* fn() { | ||
yield* [Promise.resolve("ok")] // CreateAsyncFromSyncIterator | ||
} |
6 changes: 6 additions & 0 deletions
6
...generator-functions/test/fixtures/yield-star/create-async-from-sync-iterator/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": ["proposal-async-generator-functions"], | ||
"parserOpts": { | ||
"allowReturnOutsideFunction": true | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...nc-generator-functions/test/fixtures/yield-star/create-async-from-sync-iterator/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
".