toAsyncIterable: remove Promise.resolve call that's already done by the JS runtime #8913
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.
When
await-ing something oryield-ing something from an async generator, one doesn't need to callPromise.resolveto cast it to a Promise. The language runtime and all library functions already to that for us. Examples:Library functions that accept promises always accept other values, too:
This resolves to
[ 1, 2, 3 ]after 1 second. The1and2values are wrapped withPromise.resolveautomatically.awaitalso accepts both promises and non-promises:and finally, async generator can yield non-promise values and yet the resulting iterator will always return promises:
Every well-designed API that accepts promises should behave this way. See the W3C guide on writing promise-using specs.
This patch also fixes a bug: if the thing passed to
toAsyncIterableis an array of thenables that are not instances of the nativePromise, e.g., Bluebird promises or objects from some polyfill, the resulting async iterable will "follow" the values of these thenables:Before:
console.logprints some objects with athenmethodAfter:
console.logprints values1and2Idea for a followup PR:
The
toAsyncIterablefunction does more redundant work that's already done by thefor awaitloop. For example,for awaitaccepts a synchronous iterable or iterator:Therefore, any array, map, or sync iterator is already a valid async iterable, although it's not an async iterator. What does the
toAsyncIterablefunction want to return? The name suggests that it wants to return async iterable, but the unit tests test for async iterator, which is a subset.