You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/11-async/05-promise-api/article.md
+26-2
Original file line number
Diff line number
Diff line change
@@ -217,6 +217,29 @@ Promise.race([
217
217
The first promise here was fastest, so it became the result. After the first settled promise "wins the race", all further results/errors are ignored.
218
218
219
219
220
+
## Promise.any
221
+
222
+
Similar to `Promise.race`, but waits only for the first fulfilled promise and gets its result. If all of the given promises are rejected, then the returned promise is rejected.
223
+
224
+
The syntax is:
225
+
226
+
```js
227
+
let promise = Promise.any(iterable);
228
+
```
229
+
230
+
For instance, here the result will be `1`:
231
+
232
+
```js run
233
+
Promise.any([
234
+
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Whoops!")), 1000)),
235
+
new Promise((resolve, reject) => setTimeout(() => resolve(1), 2000)),
236
+
new Promise((resolve, reject) => setTimeout(() => resolve(3), 3000))
237
+
]).then(alert); // 1
238
+
```
239
+
240
+
The first promise here was fastest, but it was rejected, so the second promise became the result. After the first fulfilled promise "wins the race", all further results are ignored.
241
+
242
+
220
243
## Promise.resolve/reject
221
244
222
245
Methods `Promise.resolve` and `Promise.reject` are rarely needed in modern code, because `async/await` syntax (we'll cover it [a bit later](info:async-await)) makes them somewhat obsolete.
@@ -279,7 +302,8 @@ There are 5 static methods of `Promise` class:
279
302
- `status`: `"fulfilled"` or `"rejected"`
280
303
- `value` (if fulfilled) or `reason` (if rejected).
281
304
3. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome.
282
-
4. `Promise.resolve(value)` -- makes a resolved promise with the given value.
283
-
5. `Promise.reject(error)` -- makes a rejected promise with the given error.
305
+
4. `Promise.any(promises)` -- waits for the first promise to fulfill, and its result becomes the outcome. If all of the given promises rejects, it becomes the error of `Promise.any`.
306
+
5. `Promise.resolve(value)` -- makes a resolved promise with the given value.
307
+
6. `Promise.reject(error)` -- makes a rejected promise with the given error.
284
308
285
309
Of these five, `Promise.all` is probably the most common in practice.
0 commit comments