在node.js中永远重复这组操作

6 asynchronous node.js promise

我正在使用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())
    ;
}
Run Code Online (Sandbox Code Playgroud)

我想做的是让这套行动永远重演.如何在node.js中完成?

//make following actions repeat forever
do_X() 
Delay(1000)
do_Y()
Delay(1000)
Run Code Online (Sandbox Code Playgroud)

编辑:我开始使用重复队列解决问题的答案赏金.

Mik*_*ike 3

只需使用递归

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) => { ... });
}
Run Code Online (Sandbox Code Playgroud)