Skip to content

Instantly share code, notes, and snippets.

@alanmquach
Created May 13, 2015 22:10
Show Gist options
  • Save alanmquach/17e07c25e205be2b9a4f to your computer and use it in GitHub Desktop.
Save alanmquach/17e07c25e205be2b9a4f to your computer and use it in GitHub Desktop.
Async composition
// Pyramid of doom
function go(input) {
// step 1
doSomethingAsync(input, function (result) {
// step 2
doSomethingElse(result, function (result2) {
// step 3
doYetAnotherThing(result2, function (result3) {
moveOnWithLife(result3);
});
});
});
}
// Happyness
function go(input) {
async.seq(
function (initial, next) {
// step 1
doSomethingAsync(initial, function (result) {
next(null, result);
});
},
function (prevResult, next) {
// step 2
doSomethingElse(prevResult, function (result) {
next(null, result);
});
},
function (prevResult, next) {
// step 3
doYetAnotherThing(prevResult, function (result) {
next(null, result);
});
}
)(input, function (finalCountdown) {
moveOnWithLife(finalCountdown);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment