Created
March 9, 2016 04:01
-
-
Save jooyunghan/1c88409beb4f01816c05 to your computer and use it in GitHub Desktop.
Promise helper which works like mapM
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
/** | |
* @template T, S | |
* @param {[T]} items | |
* @param {function(T):Promise<S>} f | |
* @return {Promise<[S]>} | |
*/ | |
function traverse(items, f) { | |
if (items.length == 0) { | |
return Promise.resolve([]); | |
} | |
var item = items.shift(); | |
return f(item).then(v => traverse(items, f).then(rest => [v].concat(rest))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment