Search code examples
javascriptnode.jscallbackpromisewhen-js

Callback pyramid even with When Promise


I have 3 functions that I want to execute one after another, only when the previous function has finished its task. I use When Promise library for this,

function taskA(){
    var d = when.defer();
    d.resolve();
    return d.promise;
}
function taskB(){
    var d = when.defer();
    d.resolve();
    return d.promise;
}
function taskC(){
    var d = when.defer();
    d.resolve();
    return d.promise;
}

taskA().then(function(){
    taskB().then(function(){
        taskC().then(function(){
}); }); });

Is this how it's supposed to be? I was under the impression I could easily avoid callbacks and its "pyramid of doom" using promises, or am I using them wrong?


Solution

  • What about

    taskA()
       .then(taskB)
       .then(taskC)
       .then(function(){});