-
-
Save robotlolita/a5b69ec2cdf819398c5b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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