How about awaiting Arrays ?

Andrea Giammarchi andrea.giammarchi at gmail.com
Sat Mar 11 23:54:56 UTC 2017


Nice one, but at that point `const all = a => Promise.all(a)` seems a
better option.

I think the point here is that we all need that and repeating the pattern
every single time feels like a very clunky experience.

`await.all` looks like a win 🎉

On Sat, Mar 11, 2017 at 6:24 PM, Axel Rauschmayer <rauschma at me.com> wrote:

> I like the following way of using `Promise.all()`:
>
> ```js
> const all = Promise.all.bind(Promise);
>
> const allTheThings = await all([pa, pb, pc]);
> ```
>
>
> On 3 Mar 2017, at 13:43, Andrea Giammarchi <andrea.giammarchi at gmail.com>
> wrote:
>
> Not the first time I accidentally type the following:
>
> ```js
> const allTheThings = await [pa, pb, pc];
> ```
>
> I am assuming that JS will implicitly realize that'd be a
> `Promise.all([pa, pb, pc])` call but nope.
>
> Then I also realize it'd be cool to have other shortcuts too that play
> nice with arrays such:
>
> ```js
> Array.prototype.all = function all() { return Promise.all(this); };
> Array.prototype.race = function race() { return Promise.race(this); };
>
> // with the lovely addiction of ...
> Array.prototype.any = function any() { return Promise.any(this); };
>
> // with Promise.any being (sorry it was a tweet)
> Promise.any = $ => new Promise((D,E,A,L) => {
>   z = [];
>   _ = $.map(($, i) => Promise.resolve($)
>         .then(D, O => { z[i] = O; --_ || E(z) })
>   ).length
> });
> ```
>
> So that ...
> ```js
> const allTheThings = await [pa, pb, pc].all();
> ```
>
> Yay? Nay? Silly? no-way?
>
> I thought it was worth it to point this out.
>
> Best Regards
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de
> dr-axel.de
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170311/8953c040/attachment-0001.html>


More information about the es-discuss mailing list