Skip to content

Instantly share code, notes, and snippets.

@markmur
Last active February 16, 2020 14:49
Show Gist options
  • Select an option

  • Save markmur/7cb81790bfea2e9e97fd9e068dd94aa5 to your computer and use it in GitHub Desktop.

Select an option

Save markmur/7cb81790bfea2e9e97fd9e068dd94aa5 to your computer and use it in GitHub Desktop.
Execute async functions in series
/**
* Execute an array of async functions in series
*/
const sequential = async fns => {
const AsyncFunction = (async () => {}).constructor
// Use "some" rather than "every" here to abort faster
if (!fns.some(fn => fn instanceof AsyncFunction)) {
throw new TypeError('`fns` argument should be an array of Async functions')
}
const responses = []
for (const fn of fns) {
const response = await fn()
responses.push(response)
}
return responses
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment