Skip to content

Instantly share code, notes, and snippets.

@pguillory
Created February 22, 2011 22:16
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 pguillory/839545 to your computer and use it in GitHub Desktop.
Save pguillory/839545 to your computer and use it in GitHub Desktop.
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