문제

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?

도움이 되었습니까?

해결책

What about

taskA()
   .then(taskB)
   .then(taskC)
   .then(function(){});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top