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 1/5] 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. From e289aba4ee76de0ae2655f5c947721fa74b46265 Mon Sep 17 00:00:00 2001 From: Mustafa Kemal Tuna <12192118+lumosmind@users.noreply.github.com> Date: Fri, 20 Nov 2020 15:43:20 +0300 Subject: [PATCH 2/5] Update 1-js/11-async/05-promise-api/article.md Co-authored-by: Vse Mozhe Buty --- 1-js/11-async/05-promise-api/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/11-async/05-promise-api/article.md b/1-js/11-async/05-promise-api/article.md index fa056628..d6933ec4 100644 --- a/1-js/11-async/05-promise-api/article.md +++ b/1-js/11-async/05-promise-api/article.md @@ -237,7 +237,7 @@ Promise.race([ ]).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. +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. ## Promise.resolve/reject From 0e79e06444d69d8036b54d5f17e44e453be7248c Mon Sep 17 00:00:00 2001 From: Mustafa Kemal Tuna <12192118+lumosmind@users.noreply.github.com> Date: Wed, 25 Nov 2020 10:30:19 +0300 Subject: [PATCH 3/5] example is fixed and summary is extended example is fixed and summary is extended due to feedbacks --- 1-js/11-async/05-promise-api/article.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/1-js/11-async/05-promise-api/article.md b/1-js/11-async/05-promise-api/article.md index d6933ec4..989ab02f 100644 --- a/1-js/11-async/05-promise-api/article.md +++ b/1-js/11-async/05-promise-api/article.md @@ -230,7 +230,7 @@ let promise = Promise.any(iterable); For instance, here the result will be `1`: ```js run -Promise.race([ +Promise.any([ 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)) @@ -302,7 +302,8 @@ There are 5 static methods of `Promise` class: - `status`: `"fulfilled"` or `"rejected"` - `value` (if fulfilled) or `reason` (if rejected). 3. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome. -4. `Promise.resolve(value)` -- makes a resolved promise with the given value. -5. `Promise.reject(error)` -- makes a rejected promise with the given error. +4. `Promise.any(promises)` -- waits for the first promise to fulfill, and its result/error becomes the outcome. +5. `Promise.resolve(value)` -- makes a resolved promise with the given value. +6. `Promise.reject(error)` -- makes a rejected promise with the given error. Of these five, `Promise.all` is probably the most common in practice. From 5f040588ff75fd627e5238fab1d04f6e06702ba4 Mon Sep 17 00:00:00 2001 From: Mustafa Kemal Tuna <12192118+lumosmind@users.noreply.github.com> Date: Wed, 25 Nov 2020 10:37:06 +0300 Subject: [PATCH 4/5] Summary section is fixed first fulfilled promise can not return error. --- 1-js/11-async/05-promise-api/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/11-async/05-promise-api/article.md b/1-js/11-async/05-promise-api/article.md index 989ab02f..617dc11d 100644 --- a/1-js/11-async/05-promise-api/article.md +++ b/1-js/11-async/05-promise-api/article.md @@ -302,7 +302,7 @@ There are 5 static methods of `Promise` class: - `status`: `"fulfilled"` or `"rejected"` - `value` (if fulfilled) or `reason` (if rejected). 3. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome. -4. `Promise.any(promises)` -- waits for the first promise to fulfill, and its result/error becomes the outcome. +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`. 5. `Promise.resolve(value)` -- makes a resolved promise with the given value. 6. `Promise.reject(error)` -- makes a rejected promise with the given error. From b73c2a452599ff3bb395ed94e2b85abaae229796 Mon Sep 17 00:00:00 2001 From: Mustafa Kemal Tuna <12192118+lumosmind@users.noreply.github.com> Date: Wed, 25 Nov 2020 10:38:23 +0300 Subject: [PATCH 5/5] typo --- 1-js/11-async/05-promise-api/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/11-async/05-promise-api/article.md b/1-js/11-async/05-promise-api/article.md index 617dc11d..83c65e2e 100644 --- a/1-js/11-async/05-promise-api/article.md +++ b/1-js/11-async/05-promise-api/article.md @@ -302,7 +302,7 @@ There are 5 static methods of `Promise` class: - `status`: `"fulfilled"` or `"rejected"` - `value` (if fulfilled) or `reason` (if rejected). 3. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome. -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`. +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`. 5. `Promise.resolve(value)` -- makes a resolved promise with the given value. 6. `Promise.reject(error)` -- makes a rejected promise with the given error.