Asynchronous Loop of jQuery Deferreds (promises)

后端 未结 6 1936
执笔经年
执笔经年 2020-12-08 08:21

I am trying to create what I think is referred to as a \"Waterfall\". I want to sequentially process an array of async functions (jQuery promises).

Here\'s a contr

6条回答
  •  离开以前
    2020-12-08 08:57

    For a waterfall, you need an async loop:

    (function step(i, callback) {
        if (i < tasks.length)
            doTask(tasks[i]).then(function(res) {
                // since sequential, you'd usually use "res" here somehow
                step(i+1, callback);
            });
        else
            callback();
    })(0, function(){
        console.log("all done");
    });
    

提交回复
热议问题