Skip to content

Instantly share code, notes, and snippets.

@jthoms1
Created September 4, 2018 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jthoms1/04c4b308a5080f52dbfc316628e7579a to your computer and use it in GitHub Desktop.
Save jthoms1/04c4b308a5080f52dbfc316628e7579a to your computer and use it in GitHub Desktop.
Simple promise waterfall.
function waterFallExec<T, S>(listOfItems: T[], func: (item: T) => Promise<S>): Promise<S[]> {
const results: S[] = []
return listOfItems.reduce(function (lastPromise, item) {
return lastPromise.then(function (res) {
return func(item).then(function (result) {
res.push(result);
return res;
});
});
}, Promise.resolve(results));
}
const promiseOfResults = waterFallExec(['1', '2', '3'], (item) => {
return Promise.resolve(
parseInt(item, 10)
);
});
promiseOfResults.then((results) => {
console.log(results);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment