Created
May 28, 2020 01:37
-
-
Save markreid/94e53bc42f84f5068b25a9cd29edffe2 to your computer and use it in GitHub Desktop.
sequential async
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
/** | |
* 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