Skip to content

Instantly share code, notes, and snippets.

@markreid
Created May 28, 2020 01:37
Show Gist options
  • Save markreid/94e53bc42f84f5068b25a9cd29edffe2 to your computer and use it in GitHub Desktop.
Save markreid/94e53bc42f84f5068b25a9cd29edffe2 to your computer and use it in GitHub Desktop.
sequential async
/**
* Run a series of async/promisifed functions sequentially,
* returning an array of results.
* Throws any exceptions.
*
* example:
* const values = [1, 2, 3, 4, 5];
* const callback = async (value) => await someAsyncThing(value)
* const result = await sequentialAsync(values, callback);
*
* @param {Iterable} values
* @param {Function} callback
* @return {Array} array of results
*/
const sequentialAsync = async (iterator, callback) =>
iterator.reduce(async (acc, value) => {
const results = await acc;
return [...results, await callback(value)];
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment