Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Support for Array Prototype Iteration Methods? #134

Open
machineghost opened this issue Oct 18, 2020 · 8 comments
Open

Support for Array Prototype Iteration Methods? #134

machineghost opened this issue Oct 18, 2020 · 8 comments

Comments

@machineghost
Copy link

Any chance this proposal could get a 2.0 version that adds support for asynchronous mapping, filtering, reducing, etc.?

@Jamesernator
Copy link

What you're asking for is already at stage 2: https://github.com/tc39/proposal-iterator-helpers

@machineghost
Copy link
Author

machineghost commented Oct 18, 2020

Awesome news, thanks! It's a little unclear from that proposal that you'll be able to do:

const asyncSum = await urls.reduce(async (sum, url) => {
    const response = await fetch(url);
    const { number } = await response.json();
   return sum + number;
}, 0);

but if it does that will be a truly great language improvement :-)

@Jamesernator
Copy link

Awesome news, thanks! It's a little unclear from that proposal that you'll be able to do:

Not if urls is an Array, but you can just wrap it using AsyncIterator.from:

const asyncSum = await AsyncIterator.from(urls).reduce(async (sum, url) => {
    const response = await fetch(url);
    const { number } = await response.json();
   return sum + number;
}, 0);

@devsnek
Copy link
Member

devsnek commented Oct 18, 2020

If it's an array you can do urls.values().reduce(...)

@Jamesernator
Copy link

Jamesernator commented Oct 18, 2020

If it's an array you can do urls.values().reduce(...)

That won't work for the given example, as then the sum argument would be a promise as urls.values() is a sync iterator.

@machineghost
Copy link
Author

machineghost commented Oct 18, 2020

Seems like it would be a lot simpler to instead have:

const asyncSum = await urls.asyncReduce(async (sum, url) => {
    const response = await fetch(url);
    const { number } = await response.json();
   return sum + number;
}, 0);

... but beggars can't be choosers :-D

@karlhorky
Copy link

Added an PR with an example similar to the one from @Jamesernator to the readme of the proposal: tc39/proposal-iterator-helpers#124

codehag pushed a commit to tc39/proposal-iterator-helpers that referenced this issue Jun 23, 2021
* Add example w. conversion from array, async map

Credit to @Jamesernator : tc39/proposal-async-iteration#134 (comment)

* Remove extra await

* Disable npm test (no test script specified)

* Add .toArray() call

Ref https://github.com/tc39/proposal-iterator-helpers/pull/124/files#r569479964
@karlhorky
Copy link

Not if urls is an Array, but you can just wrap it using AsyncIterator.from

The example inspired by @Jamesernator has been accepted for the readme of https://github.com/tc39/proposal-iterator-helpers 🎉

tc39/proposal-iterator-helpers@dd76e00#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R66-R74

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants