Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Forked from joepie91/promises.js
Last active Aug 29, 2015
Embed
What would you like to do?
function pipeline(fs, val, done) {
if (fs.length === 0) done(null, val)
else fs[0](val, function(err, val) {
if (err) done(err)
else pipeline(fs.slice(1), val, done)
})
}
pipeline([
asyncFunctionOne,
function(val, next){ next(somethingSync(val)) },
asyncFunctionTwo,
function(_, next){ next(somethingElseSync()) },
asyncFunctionThree,
], null, function(err, val) {
console.log('Done')
})
/* Without Promises */
asynchronousFunctionOne(function(value){
somethingSync(value);
asynchronousFunctionTwo(function(){
somethingElseSync();
asynchronousFunctionThree(function(){
console.log("Done!");
});
});
});
/* With Promises */
Promise.try(function(){
return asynchronousFunctionOne();
}).then(function(value){
somethingSync(value);
return asynchronousFunctionTwo();
}).then(function(){
somethingElseSync();
return asynchronousFunctionThree();
}).then(function(){
console.log("Done!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment