How about awaiting Arrays ?

T.J. Crowder tj.crowder at farsightsoftware.com
Fri Mar 3 14:12:09 UTC 2017


On Fri, Mar 3, 2017 at 12:43 PM, 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.

The problem is that an array of promises is a perfectly valid promise
resolution value. (And that the proposal is now at Stage 4. :-) ) To
do this, `await` would have to treat arrays specially in a way that
promises don't. That seems like something `await` shouldn't do. You
might propose `awaitall`, `awaitany`, `awaitrace` or similar...

> 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); };

Side note: I'm fairly sure `all` isn't websafe as an `Array.prototype`
function (isn't that why ES5 used `every`?).

I don't think arrays need special promise-related functionality in the
standard lib.  But if they got it, I'd want it to be reflected in the
naming, e.g. `[].promiseAll`, `[].promiseAny`, ... Both for clarity and
for web safety (I'm fairly sure `all` on `Array.prototype` wouldn't be
web-safe).

-- T.J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170303/786e44d4/attachment.html>


More information about the es-discuss mailing list