Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sync vs. Async
// Synchronous form - 6 lines
function f() {
a()
b()
c()
d()
}
// Asynchronous form - 12 lines
function f(callback) {
a(function(err) {
if (err) return callback(err)
b(function(err) {
if (err) return callback(err)
c(function(err) {
if (err) return callback(err)
d(callback)
})
})
})
}
// Asynchronous form, broken into functions to limit nesting - 18 lines
function f(callback) {
a(function(err) {
if (err) return callback(err)
bcd(callback)
})
}
function bcd(callback) {
b(function(err) {
if (err) return callback(err)
cd(callback)
})
}
function cd(callback) {
c(function(err) {
if (err) return callback(err)
d(callback)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment