How about awaiting Arrays ?

Matthew Robb matthewwrobb at gmail.com
Fri Mar 3 15:28:10 UTC 2017


I think this conversation needs to happen but I am not sure baking it into
Array facilities makes the most sense.

In my experience with async/await I am very often needing Promise.all but
in some cases the other forms of multi promise capabilities. What if we
expanded the keyword `await.all [...]; await.race [...]` or add a new
context for `of` outside for loops but this would be limited to P.all
behavior `await of [...]`?


- Matthew Robb

On Fri, Mar 3, 2017 at 9:47 AM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> If this is what we gonna have
>
> `[].promiseAll`
>
> then I'd rather
>
> `Promise.all()`
>
>
> You made some fair point, I guess there's nothing to see here.
>
> Regards
>
>
> On Fri, Mar 3, 2017 at 2:12 PM, T.J. Crowder <tj.crowder at farsightsoftware.
> com> wrote:
>
>> 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.
>>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170303/e1ab9d38/attachment-0001.html>


More information about the es-discuss mailing list