How about awaiting Arrays ?

Axel Rauschmayer rauschma at icloud.com
Sat Mar 11 18:26:14 UTC 2017


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/d81e0277/attachment.html>


More information about the es-discuss mailing list