Created
July 2, 2014 14:51
-
-
Save briancavalier/33c346b478e8f36c1076 to your computer and use it in GitHub Desktop.
Simplified find left-most fulfilled
This file contains 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 findFirst(promises){ | |
return recurseFindFirst(0, [], promises); | |
} | |
function recurseFindFirst(i, errors, promises) { | |
if(i === promises.length) { | |
var e = new Error('All promises rejected'); | |
e.errors = errors; | |
return when.reject(e); | |
} | |
return when(promises[i]).catch(function(e) { | |
errors.push(e); | |
return recurseFindFirst(i+1, errors, promises); | |
}); | |
} | |
var testList = [ | |
when.reject(new Error('a')), | |
when.reject(new Error('b')), | |
when.resolve('c'), | |
when.reject(new Error('d')) | |
]; | |
findFirst(testList).done(console.log.bind(console)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment