Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Forked from joepie91/promises.js
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotlolita/a5b69ec2cdf819398c5b to your computer and use it in GitHub Desktop.
Save robotlolita/a5b69ec2cdf819398c5b to your computer and use it in GitHub Desktop.
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