Cyrille Cyrille - 1 year ago 63
Javascript Question

Loop on a promise indefinitely until rejection

I have a function that does some async work and returns a

, and I want to execute this function indefinitely until the promise is

Something like the following :

// ... until rejection

I made a little CodePen so I can test potential solutions :

I found several potential answers but nothing seems to work for my case.

If anyone has a solution, I'd be glad, because I just can't find anything !


Answer Source

You can do

(function loop(){

But looking at your pen it's not clear where the rejection should come from. If you want to stop repeating an operation when the user clicks a button, you can change a state in the button handling and then do

(function loop(){
          if (!stopped) loop();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download