Created
May 13, 2015 22:10
-
-
Save alanmquach/17e07c25e205be2b9a4f to your computer and use it in GitHub Desktop.
Async composition
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
// 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