The concept of iteration intuitively seems to be synchronous — when the iteration completes we will have our results ready.
There are many native ...
For further actions, you may consider blocking this person and/or reporting abuse
I think what would be really nice to have is, instead of
Promsie.map
,Array.prototype.amap
/Array.prototype.afilter
andArray.prototype.areduce
. They all return a promise. This would be really handy.This is essentially the same thing, but I think it is more intuitive to use.
What do you think about this idea?
BTW, based on your comment, you would be super interested in the new async iteration proposal
Kushan also gives a small example in his comment.
That would be interesting.
But adding something to native JS API, thats a lot to hope for :)
There are lots of other Promise utilities which will offer us handy utilities like this out of the box.
I have often seen the need for a
Promise.map
when one has to deal with concurrency of promises. As the nativePromise.all
doesn't allow for controlling how many promises are executed.I also find using the async for of loop a pretty neat way of adding concurrency to an array of promises.
Concurrency = 1
Concurrency = N
The fancy for-await-of
You can also use
for-await-of
but remember it works for async iterables, you can use them for an array of promises, but then you aren't really using them to full extent.Here's how you would create an async iterable.
The best part about this approach is again concurrency, the async generator
asyncGen
only creates a new promise when asked for, and thefor-await-of
automatically awaits at the start of for loop, resolves it and puts it as the constg
.Hey Kushan
The batching idea is neat. Didn't occur to me that you can play with the degree of concurrency.
Async iterables are pretty awesome. Just discovered them. I guess they officially landed in ES2018.
Thanks for the addendum.
Fun Fact: I was there for your talk at last year's ReactFoo. It proved helpful for my own redux saga journey.
here is what i ran into with
reduce
andPromise
.gyandeeps.com/array-reduce-async-a...