Skip to content

Instantly share code, notes, and snippets.

@pguillory
Created February 22, 2011 22:16
Show Gist options
  • Select an option

  • Save pguillory/839545 to your computer and use it in GitHub Desktop.

Select an option

Save pguillory/839545 to your computer and use it in GitHub Desktop.

Revisions

  1. pguillory created this gist Feb 22, 2011.
    41 changes: 41 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    // 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)
    })
    }