Repeat this set of actions forever in node.js

I am using node.js. I have this function that uses promises to introduce a delay between the execution of certain actions.

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()) ; } 

I want this set of actions to be repeated forever. How can this be done in node.js?

 //make following actions repeat forever do_X() Delay(1000) do_Y() Delay(1000) 

EDIT: I started dispensing answers that use a repeating queue to solve the problem.

+6
source share
2 answers

Just use recursion

 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) => { ... }); } 
+4
source
 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) => { ... }); 

}

0
source

All Articles