Created
September 4, 2018 19:08
-
-
Save jthoms1/04c4b308a5080f52dbfc316628e7579a to your computer and use it in GitHub Desktop.
Revisions
-
jthoms1 created this gist
Sep 4, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ 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); })