From 891495cd475c1bc68004311ade9ff31b7115d12b Mon Sep 17 00:00:00 2001 From: Mustafa Kemal Tuna <12192118+lumosmind@users.noreply.github.com> Date: Tue, 17 Nov 2020 07:55:57 +0300 Subject: [PATCH] Promise.any I think Promise.any() must be in this article. --- 1-js/11-async/05-promise-api/article.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/1-js/11-async/05-promise-api/article.md b/1-js/11-async/05-promise-api/article.md index 5b4930be..fa056628 100644 --- a/1-js/11-async/05-promise-api/article.md +++ b/1-js/11-async/05-promise-api/article.md @@ -217,6 +217,29 @@ Promise.race([ 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. +## Promise.any + +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. + +The syntax is: + +```js +let promise = Promise.any(iterable); +``` + +For instance, here the result will be `1`: + +```js run +Promise.race([ + new Promise((resolve, reject) => setTimeout(() => reject(new Error("Whoops!")), 1000)), + new Promise((resolve, reject) => setTimeout(() => resolve(1), 2000)), + new Promise((resolve, reject) => setTimeout(() => resolve(3), 3000)) +]).then(alert); // 1 +``` + +The first promise here was fastest, but it rejecets, so the second promise became the result. After the first fulfilled promise "wins the race", all further results are ignored. + + ## Promise.resolve/reject 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.