我正在使用node.js.我有这个函数,它使用promises在执行某些操作之间引入延迟。
function do_consecutive_action() {
Promise.resolve()
.then(() => do_X() )
.then(() => Delay(1000))
.then(() => do_Y())
.then(() => Delay(1000))
.then(() => do_X())
.then(() => Delay(1000))
.then(() => do_Y())
;
}
我想要做的是让这组动作永远重演。如何在node.js中完成?
//make following actions repeat forever
do_X()
Delay(1000)
do_Y()
Delay(1000)
编辑:我为使用重复队列来解决问题的答案开始了赏金。
答案 0 :(得分:4)
只需使用递归
function do_consecutive_action() {
Promise.resolve()
.then(() => do_X() )
.then(() => Delay(1000))
.then(() => do_Y())
.then(() => Delay(1000))
.then(() => do_consecutive_action())
// You will also want to include a catch handler if an error happens
.catch((err) => { ... });
}
答案 1 :(得分:0)
function cb(func) {
try {
func();
}
catch (e) {
do_consecutive_action();
}
}
function do_consecutive_action() {
Promise.resolve()
.then(() => cb(do_X))
.then(() => Delay(1000))
.then(() => cb(do_Y))
.then(() => Delay(1000))
.then(() => do_consecutive_action())
// You will also want to include a catch handler if an error happens
.catch((err) => { ... });
}