Skip to content

Instantly share code, notes, and snippets.

@ccnokes
Last active November 17, 2022 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccnokes/9eec2d26b7e6f3bfcb29217779ed2c42 to your computer and use it in GitHub Desktop.
Save ccnokes/9eec2d26b7e6f3bfcb29217779ed2c42 to your computer and use it in GitHub Desktop.
Takes a list of async functions and executes them one at a time. You could just use a `for of` loop for this too πŸ˜…
/**
* takes a list of async functions and executes them one at a time
*/
async function serial(
fnList: Array<() => Promise<void>>,
nextIndex?: number = 0,
) {
let current = fnList[nextIndex];
if (!current) return;
try {
await current();
} catch (err) {
console.error(err);
}
await serial(fnList, nextIndex + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment