emersonbroga
6/30/2015 - 12:19 AM

Is there a better way to work with promises and callback?

Is there a better way to work with promises and callback?

#Is there a better way to work with promises and callback?

synchronous programming pseudo example:

var result = doSomething();

var param = (result[x]) ? 'foo' : 'bar';

var otherResult = doSomethingElse(param);

var saveResult = saveSomething(otherResult);

return json(saveResult);

asynchronous programming callback pseudo example:

doSomething( function(err, result){
    var param = (result[x]) ? 'foo' : 'bar';
    doSomethingElse(param, function(err, otherResult){
        saveSomething(otherResult, function (err, saveResult){
            return json(saveResult);
        });
    });
});

###asynchronous programming promises pseudo example:

doSomething().then(function(result){
    var param = (result[x]) ? 'foo' : 'bar';
    doSomethingElse(param).then(function(otherResult){
        saveSomething(otherResult).then(function(saveResult){
            return json(saveResult);
        }, function(error){

        });
    }, function(error){

    });
}, function(error){
    
});