Last active
September 20, 2024 14:57
-
-
Save olibooty/a32caa181591e2c2951f41d8f35ffe27 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface PromiseRes <TObj>{ | |
| fulfilled: TObj[]; | |
| rejects: PromiseRejectedResult[]; | |
| } | |
| /** | |
| * Given an array of promises it will wait for every item to be resolved or rejected | |
| * and returns the fulfilled and rejected objects in two separate arrays | |
| */ | |
| const allSettledCategorised = async <TObj,>(promisesArray: Promise<TObj>[]): PromiseRes<TObj> => { | |
| const awaitedPromises = await Promise.allSettled(promisesArray); | |
| return awaitedPromises.reduce<PromiseRes<TObj>>( | |
| (acc, cur) => { | |
| if (cur.status === "fulfilled") { | |
| acc.fulfilled.push(cur.value); | |
| } else { | |
| acc.rejects.push(cur); | |
| } | |
| return acc; | |
| }, | |
| { fulfilled: [], rejects: [] } | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment