Skip to content

Instantly share code, notes, and snippets.

@jooyunghan
Created March 9, 2016 04:01
Show Gist options
  • Save jooyunghan/1c88409beb4f01816c05 to your computer and use it in GitHub Desktop.
Save jooyunghan/1c88409beb4f01816c05 to your computer and use it in GitHub Desktop.
Promise helper which works like mapM
/**
* @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