Skip to content

Instantly share code, notes, and snippets.

@KhalilZaidoun
Created February 25, 2018 12:49
Show Gist options
  • Save KhalilZaidoun/b4e56a9f2a8c903b66ee53365de3f805 to your computer and use it in GitHub Desktop.
Save KhalilZaidoun/b4e56a9f2a8c903b66ee53365de3f805 to your computer and use it in GitHub Desktop.
Apply asynchronous function to each item in array
/**
*
* @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