Created
September 4, 2018 19:08
-
-
Save jthoms1/04c4b308a5080f52dbfc316628e7579a to your computer and use it in GitHub Desktop.
Simple promise waterfall.
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
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