Javascript 异步投票
我想在Asynquence中实现轮询。我已经能够通过递归进行轮询,但是一旦轮询完成,我就无法控制主序列 示例代码:Javascript 异步投票,javascript,asynchronous,recursion,Javascript,Asynchronous,Recursion,我想在Asynquence中实现轮询。我已经能够通过递归进行轮询,但是一旦轮询完成,我就无法控制主序列 示例代码: function countdown(count){ return ASQ().then(function(done){ // This is where the call http call would be made... console.log("Countdown: " +count); if (count){
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阶段未打印:
我已经创建了一个JSFIDLE
我肯定我只是做错了一些简单的事情,有人能给我一些帮助吗
当我学习asnquence时,我在调用时注意到类似的行为(即,控件没有返回到主序列)。然后(我的_函数),其中我的_函数也创建了一个序列。我发现调用.seq()是正确的做法,但它似乎不适用于递归…似乎不能从该
回调返回序列。改用seq
:
function countdown(count){
return ASQ().seq(function(){
addRow("log2", "Countdown: " +count);
if (count) {
return countdown(count-1);
} else {
return ASQ();
}
});
}
()我不太了解Asquience,但我的赌注是返回ing倒计时(count-1)
。然后()
回调。非常感谢-而且非常快!