Created
February 25, 2018 12:49
-
-
Save KhalilZaidoun/b4e56a9f2a8c903b66ee53365de3f805 to your computer and use it in GitHub Desktop.
Apply asynchronous function to each item in array
This file contains 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
/** | |
* | |
* @param items An array of items. | |
* @param fn A function that accepts an item from the array and returns a promise. | |
* @returns {Promise} | |
*/ | |
export default function forEachPromise(items = [], fn = Promise.resolve) { | |
return items.reduce( | |
(promise, item) => promise.then(() => fn(item)), | |
Promise.resolve() | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment