Created
January 14, 2015 09:23
-
-
Save jmcanterafonseca/ced8fbb8debb9a6b51d1 to your computer and use it in GitHub Desktop.
Promise.sequential
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
Promise.sequential = function(runnables) { | |
return new Promise(function(resolve, reject) { | |
var results = []; | |
runnables.reduce(function(sequence, aRunnable) { | |
return sequence.then(function() { | |
return aRunnable.run(); | |
}).then(function(data) { | |
results.push(data); | |
if (results.length === runnables.length) { | |
resolve(results); | |
} | |
}); | |
}, Promise.resolve()); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment