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