Run a list of functions in series, concurrently, or allSettled.
npm install @phothinmaung/resolvesThe package lets you run a list of functions in series, concurrently, or using an allSettled pattern, returning an array of Promise-resolved results. It is useful for managing multiple asynchronous or synchronous tasks with different strategies.
- Run functions in series (one after another)
- Run functions concurrently (all at once)
- Run functions with allSettled (wait for all to finish, regardless of success/failure)
``bash`
npm i @phothinmaung/resolves
`js
const resolves = require("@phothinmaung/resolves");
const asyncFunction = async (str) => {
return await new Promise((resolve) => resolve(str));
};
const syncFunction = (str) => str;
const syncFunctionNum = (num) => num;
const rit = resolves([
[asyncFunction, "Result 1"],
[syncFunction, "Result 2"],
[syncFunctionNum, 3],
]);
rit.series().then((res) => {
console.log(result-1 : ${res[0]}); // result-1 : Result 1result-2 : ${res[1]}
console.log(); // result-2 : Result 2result-3 : ${res[2]}
console.log(); // result-3 : 3`
});
`ts
import resolves from "@phothinmaung/resolves";
const asyncFunction = async (str: string): Promise
return await new Promise((resolve) => resolve(str));
};
const syncFunction = (str: string): string => str;
const syncFunctionNum = (num: number): number => num;
const rit = resolves<[Promise
[asyncFunction, "Result 1"],
[syncFunction, "Result 2"],
[syncFunctionNum, 3],
]);
const [a1, b1, c1] = await rit.series();
const [a2, b2, c2] = await rit.concurrent();
const [a3, b3, c3] = await rit.allSettled();
// Result 1 - Result 2 - 3
console.log(${a1} - ${b1} - ${c1});${a2} - ${b2} - ${c2}
console.log();${a3} - ${b3} - ${c3}
console.log();`
resolves
parameters :
- An array of tuples : [[(...args:any[])=>any,...args:any[]]]
The first place of tuple is function and rest are parameters of that function.
- Number of milliseconds(optional) : time?:number The amount of time to wait before resolving the promise. Defaults to 500ms.
returns :
An object with series, concurrent, and allSettled` properties. Each property is a function that takes no arguments and returns a promise. The promise resolves with the results of running the functions in the specified manner.
[ISC][file-license] © [Pho Thin Mg][ptm]
[file-license]: LICENSE
[ptm]: https://github.com/phothinmg