Last active
February 16, 2020 14:49
-
-
Save markmur/7cb81790bfea2e9e97fd9e068dd94aa5 to your computer and use it in GitHub Desktop.
Execute async functions in series
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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