From c1db7d827b22eb0574b33cb5d2a551238e67307a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Francisco=20Franco=20Can=C3=A7ado=20Martins?= Date: Tue, 6 May 2025 09:51:28 -0300 Subject: [PATCH] docs: remove outdated parts about Promise.allSettled --- 1-js/11-async/05-promise-api/article.md | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/1-js/11-async/05-promise-api/article.md b/1-js/11-async/05-promise-api/article.md index 7be84ce2cc..53db1a7a61 100644 --- a/1-js/11-async/05-promise-api/article.md +++ b/1-js/11-async/05-promise-api/article.md @@ -119,8 +119,6 @@ So we are able to pass ready values to `Promise.all` where convenient. ## Promise.allSettled -[recent browser="new"] - `Promise.all` rejects as a whole if any promise rejects. That's good for "all or nothing" cases, when we need *all* results successful to proceed: ```js @@ -171,28 +169,6 @@ The `results` in the line `(*)` above will be: So for each promise we get its status and `value/error`. -### Polyfill - -If the browser doesn't support `Promise.allSettled`, it's easy to polyfill: - -```js -if (!Promise.allSettled) { - const rejectHandler = reason => ({ status: 'rejected', reason }); - - const resolveHandler = value => ({ status: 'fulfilled', value }); - - Promise.allSettled = function (promises) { - const convertedPromises = promises.map(p => Promise.resolve(p).then(resolveHandler, rejectHandler)); - return Promise.all(convertedPromises); - }; -} -``` - -In this code, `promises.map` takes input values, turns them into promises (just in case a non-promise was passed) with `p => Promise.resolve(p)`, and then adds `.then` handler to every one. - -That handler turns a successful result `value` into `{status:'fulfilled', value}`, and an error `reason` into `{status:'rejected', reason}`. That's exactly the format of `Promise.allSettled`. - -Now we can use `Promise.allSettled` to get the results of *all* given promises, even if some of them reject. ## Promise.race