1

Problem

The p-any package suggests that it is a replacement for Promise.race given this link as a reason. As p-any suggest, it does seem it like is a better option in most scenarios, however Promise.any seems to do the same thing.

My understanding

Promise.race takes an Iterable of promises and returns the first promise that completes with either an error or resolution.

Promise.any returns the first promise that resolves, and ignores failures.

Help

Is my understanding correct? Do p-any and Promise.any do the same thing? Thanks for your help

Community
  • 1
  • 1
Shane Mendez
  • 160
  • 9

1 Answers1

1

If your question is whether p-any's any function and the standard Promise.any function do the same thing, then based on p-any's documentation, yes, they do. They both get fulfilled when the first promise from the iterable gets fulfilled, or get rejected with an AggregateError if all of the promises from the iterable get rejected.

Promise.any is still just a Stage 3 proposal, though it's likely to hit Stage 4 very soon. p-any and similar presumably predate that effort slightly.

This chart (I wish SO's markdown allowed tables!) may be useful for understanding the different Promise combinators:

+−−−−−−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−+
| Name               | Description                                     | Added In         |
+−−−−−−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−+
| Promise.allSettled | does not short−circuit                          | ES2020           |
| Promise.all        | short−circuits when an input value is rejected  | ES2015           |
| Promise.race       | short−circuits when an input value is settled   | ES2015           |
| Promise.any        | short−circuits when an input value is fulfilled | Stage 3 proposal |
+−−−−−−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+

This article the proposal links to may as well.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hi, is it possbile to polyfill Promise.any for "old" browers ? I tried but failed. – Qiulang Feb 07 '21 at 03:10
  • @Qiulang - Yes, all of the promise combinators can be polyfilled. There are [lots of polyfills for `Promise.any`](https://duckduckgo.com/?q=%22Promise.any%22+polyfill), including ones from [`es-shims`](https://github.com/es-shims/Promise.any) and [`core-js`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.any.js). – T.J. Crowder Feb 07 '21 at 08:03
  • I have not been able to make it work. Maybe you can check my question here https://stackoverflow.com/questions/66044132/is-it-possible-to-break-away-from-await-promise-all-when-any-promise-has-fulfill – Qiulang Feb 07 '21 at 09:10
  • I did manage to upgrade babel7 but now even my chrome 88 failed to work for Promise.any() I have gave up. – Qiulang Feb 07 '21 at 10:14