gpt4 book ai didi

javascript - 异步轮询

转载 作者:行者123 更新时间:2023-11-30 17:00:53 24 4
gpt4 key购买 nike

我想在 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) 时注意到类似的行为(即控制没有返回到主序列),其中 my_function 也创建了一个序列。我发现调用 .seq() 是正确的做法,但它似乎不适用于递归...

最佳答案

看来你不能从那个 then 回调中返回一个序列。使用 seq 代替:

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

( updated demo )

关于javascript - 异步轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28892206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com