A tiny script to run async functions aka promises one after the other
Import package:
import { runSequence } from "@klw/node-sequential-promises";
Your async functions must follow a few rules:
- If it's successful, it must
return Promise.resolve()
- If it fails, it must
return Promise.reject()
orreturn Promise.reject("Your Error Message Here")
orreturn Promise.reject(new Error("Your Error Message Here"))
const myAsyncFunc1 = async () => {
if (1 + 1 = 2) {
return Promise.resolve();
} else {
return Promise.reject("Your Error Message Here");
}
}
Run your async functions on the order you like:
const result = await runSequence([myAsyncFunc1, myAsyncFunc2, myAsyncFunc3]);
node-sequential-promises
will run them one after the other.
- If one should fail, it will stop running all others.
- Even if it fails, you will get a resolved result.
A positive result:
{
success: true,
started: [0, 1, 2],
fulfilled: [0, 1, 2],
}
A negative result (2nd async function failed):
{
success: false,
started: [0, 1],
fulfilled: [0],
errorMessage: "Your Error Message Here",
}