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

kdex kdex at kdex.de
Wed Oct 26 07:30:02 UTC 2016


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.




More information about the es-discuss mailing list