Proposal: `await [p1, p2]` (equivalent to `await Promise.all([p1, p2])`)

Olivier Lalonde olalonde at gmail.com
Wed Oct 26 07:42:01 UTC 2016


Oh, I didn't realize `await` could be used on non promises :( Never mind, I
guess!

On Wed, Oct 26, 2016 at 12:30 AM, kdex <kdex at kdex.de> wrote:

> This change is not backwards-compatible. Note that the semantics of the
> following example would change:
>
> ```js
> (async () => {
>    const result = await [new Promise(resolve => {
>      setTimeout(resolve, 1000);
>    })];
> })();
> ```
>
> Now, what should `result` contain?
>
> `await` can already be used on non-`Promise`s. In case of arrays, the
> completion value will just be the same array of `Promise`s, *not* an array
> of
> their corresponding completion values.
>
> On Wednesday, October 26, 2016 12:11:36 AM CEST Olivier Lalonde wrote:
> > I have no idea if this has been discussed already but I thought it'd be
> > nice to have the following syntax sugar for async/await.
> >
> > The idea would be that any array that follows `await` would be wrapped
> into
> > a Promise.all() call. For example, `await [p1,p2]` would be the
> equivalent
> > of `await Promise.all([p1,p2])`.
> >
> > It would make some more complex expressions easier to read:
> >
> > ```
> >   const downloads = await Promise.all(regs.map(reg =>
> client.get(reg.url, {
> >     responseType: 'arraybuffer',
> >   })))
> > ```
> >
> > vs
> >
> > ```
> >   const downloads = await regs.map(reg => client.get(reg.url, {
> >     responseType: 'arraybuffer',
> >   }))
> > ```
> >
> > In many async/await code bases, it might remove the last use case for
> > typing `Promise` directly.
>
>
>


-- 
- Oli

Oli Lalonde
http://www.syskall.com <-- connect with me!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161026/3bbdcb9a/attachment.html>


More information about the es-discuss mailing list