Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Await promises from sync iterators with
for-await
(#13824)
Co-authored-by: Mickey Rose <lightmare@users.noreply.github.com>
- Loading branch information
1 parent
6029252
commit b9ea196
Showing
14 changed files
with
186 additions
and
41 deletions.
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
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
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,67 @@ | ||
/* @minVersion 7.15.9 */ | ||
|
||
export default function _asyncIterator(iterable) { | ||
var method, | ||
async, | ||
sync, | ||
retry = 2; | ||
|
||
if (typeof Symbol !== "undefined") { | ||
async = Symbol.asyncIterator; | ||
sync = Symbol.iterator; | ||
} | ||
|
||
while (retry--) { | ||
if (async && (method = iterable[async]) != null) { | ||
return method.call(iterable); | ||
} | ||
if (sync && (method = iterable[sync]) != null) { | ||
return new AsyncFromSyncIterator(method.call(iterable)); | ||
} | ||
|
||
async = "@@asyncIterator"; | ||
sync = "@@iterator"; | ||
} | ||
|
||
throw new TypeError("Object is not async iterable"); | ||
} | ||
|
||
function AsyncFromSyncIterator(s) { | ||
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); | ||
} |
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); | ||
} |
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
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
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