异步轮询

Polling in Asynquence

我想在 asynquense 中实现轮询。我已经能够通过递归进行轮询,但是轮询完成后我无法将控制权返回到主序列。

示例代码:

function countdown(count){
    return ASQ().then(function(done){
        //  This is where the call http call would be made...
        console.log("Countdown: " +count);
        if (count){
            return countdown(count-1);
        }
        done();
    })
}
function viewSvg(visId) {
    console.log("viewSvg");

    ASQ()
        .then(function(done) {
            console.log("Stage 1");
            done();
        })
        .val(4)
        .seq(countdown)
        .then(function(done){
            console.log("Stage 5");
            done();
        });

产生结果:

Stage 1
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Countdown: 0

请注意第 5 阶段未打印:

我创建了一个 jsFiddle here

我确定我只是做错了一些简单的错误,任何人都可以给我一些帮助吗?

当我学习 asnquence 时,我在调用 .then(my_function) where my_function 还创建了一个序列。我发现调用 .seq() 是正确的做法,但它似乎不适用于递归...

看来您不能 return 来自 then 回调的序列。使用 seq 代替:

function countdown(count){
    return ASQ().seq(function(){
        addRow("log2", "Countdown: " +count);
        if (count) {
            return countdown(count-1);
        } else {
            return ASQ();
        }
    });
}

(updated demo)