4

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?

laggingreflex
  • 32,948
  • 35
  • 141
  • 196

1 Answers1

4

What about

taskA()
   .then(taskB)
   .then(taskC)
   .then(function(){});
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • 1
    This is because promises unwrap. When you return a promise from a `.then` handler, it will wait until that promise is resolved before doing then ext one. – Benjamin Gruenbaum Apr 08 '14 at 18:02