Skip to content

Instantly share code, notes, and snippets.

@olibooty
Last active September 20, 2024 14:57
Show Gist options
  • Select an option

  • Save olibooty/a32caa181591e2c2951f41d8f35ffe27 to your computer and use it in GitHub Desktop.

Select an option

Save olibooty/a32caa181591e2c2951f41d8f35ffe27 to your computer and use it in GitHub Desktop.
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