在Node.js中执行重复的异步操作
我有一个我希望重复执行的功能。每次滴答声结束时,我想在3000毫秒的时间内再次触发它。如果勾选失败,我想再暂停1000毫秒,然后再试一次。我无法使用setInterval,因为我不知道完成勾号需要多长时间 以下是我实现这一目标的代码:在Node.js中执行重复的异步操作,node.js,async-await,Node.js,Async Await,我有一个我希望重复执行的功能。每次滴答声结束时,我想在3000毫秒的时间内再次触发它。如果勾选失败,我想再暂停1000毫秒,然后再试一次。我无法使用setInterval,因为我不知道完成勾号需要多长时间 以下是我实现这一目标的代码: const loop = async () => { try { console.log('Starting operation... '); await tick(); } catch (error) { console.e
const loop = async () => {
try {
console.log('Starting operation... ');
await tick();
} catch (error) {
console.error(error);
await sleep(1000);
}
setTimeout(loop, 3000);
};
loop();
不幸的是,这在运行几天后停止工作。我认为我的堆栈有问题
在Node.js中,建议以什么方式运行这样的异步操作 当前函数每3秒运行一次循环,无论它是否失败 我重写了一点。这应该行得通
const loop = async () => {
try {
console.log('Starting operation... ');
await tick();
setTimeout(loop, 3000);
} catch (error) {
console.error(error);
setTimeout(loop, 1000);
}
}
loop()
上述代码的顺序如下:
第一轮滴答声
如果勾选成功,请在3秒内再次运行循环
如果勾选失败,请在1秒内再次运行循环
@阿贾克斯。谢谢你。