Created
July 16, 2015 08:51
-
-
Save selfish/fa87dc878e0b5d3c3e56 to your computer and use it in GitHub Desktop.
Bluebird Promise Sequences
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
var Promise = require('bluebird'); | |
// waterfall | |
Promise.sequence = function (tasks) { | |
var current = Promise.cast(); | |
for (var k = 0; k < tasks.length; ++k) { | |
current = current.then(tasks[k]); | |
} | |
return current; | |
}; | |
// sequence without passing (waterfalling) results to the next item | |
Promise.sequence = function(tasks) { | |
var current = Promise.cast(); | |
for (var k = 0; k < tasks.length; ++k) { | |
current = current.thenReturn().then(tasks[k]); | |
} | |
return current.thenReturn(); | |
}; | |
// Waterfall but returning all results instead of the last one | |
Promise.sequence = function(tasks) { | |
var current = Promise.cast(), results = []; | |
for (var k = 0; k < tasks.length; ++k) { | |
results.push(current = current.then(tasks[k])); | |
} | |
return Promise.all(results); | |
}; | |
// Sequence without waterfalling, returning all results | |
Promise.sequence = function(tasks) { | |
var current = Promise.cast(), results = []; | |
for (var k = 0; k < tasks.length; ++k) { | |
results.push(current = current.thenReturn().then(tasks[k])); | |
} | |
return Promise.all(results); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment