Skip to content

Instantly share code, notes, and snippets.

@jpina
Created November 25, 2015 11:05
Show Gist options
  • Save jpina/4ab53bb25466d40b4f2d to your computer and use it in GitHub Desktop.
Save jpina/4ab53bb25466d40b4f2d to your computer and use it in GitHub Desktop.
08 Why (not) callbacks?

Why (not) callbacks?

asyncCall(function(err, data1){
    if(err) return callback(err);       
    anotherAsyncCall(function(err2, data2){
        if(err2) return calllback(err2);
        oneMoreAsyncCall(function(err3, data3){
            if(err3) return callback(err3);
            // are we done yet?
        });
    });
});

VS

 asyncCall()
.then(function(data1){
    // do something...
    return anotherAsyncCall();
})
.then(function(data2){
    // do something...  
    return oneMoreAsyncCall();    
})
.then(function(data3){
   // the third and final async response
})
.fail(function(err) {
   // handle any error resulting from any of the above calls    
})
.done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment