JavaScript Loop on a promise indefinitely until rejection

I have a function that does some async work and returns a Promise, and I want to execute this function indefinitely until the promise is rejected.

Something like the following :

doSomethingAsync().
    .then(doSomethingAsync)
    .then(doSomethingAsync)
    .then(doSomethingAsync)
    // ... until rejection

I made a little CodePen so I can test potential solutions : http://codepen.io/JesmoDrazik/pen/pbAovZ?editors=0011

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 !

Thanks.

Answer:1

You can do

(function loop(){
     doSomethingAsync().then(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(){
     doSomethingAsync().then(function(){
          if (!stopped) loop();
     });
})();
Answer:2

Made a modification to your codepen

var FAKE_COUNT = 0;

function doSomething() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('doing something async: ' + FAKE_COUNT)
      if (FAKE_COUNT < 10) {
        resolve();
      } else {
        reject();
      }
      FAKE_COUNT ++;
    }, 1000);
  });
}

const button = document.querySelector('.js-button');

button.addEventListener('click', () => {
  // maybe we can do something here ?
})

function test() {
  doSomething().then(test).catch(() => {
    console.log("Rejected")
  });
}

test();

FAKE_COUNT just becomes the flag, so if you want to stop the promise with a click instead of a count you could just make it a bool and check that when executing the async task

Answer:3

I have image tiles on my website, for want of a better description. On mobile, I would like a single tap on one of these tiles to bring up some more text (describing the content of that section), ...

I have image tiles on my website, for want of a better description. On mobile, I would like a single tap on one of these tiles to bring up some more text (describing the content of that section), ...

  1. best way to make hyperlink

I have tried the following example of ag-grid implementation: Example - Multiple Row Selection . Particulary Example - Multiple Row Selection. When I select the rows with shift in Chrome-Mozilla-Opera,...

I have tried the following example of ag-grid implementation: Example - Multiple Row Selection . Particulary Example - Multiple Row Selection. When I select the rows with shift in Chrome-Mozilla-Opera,...

I wanted to get the first image returned from an image search for the place in question and then assign that as the src for an image tag. I looked up the API for Google Image search which is ...

I wanted to get the first image returned from an image search for the place in question and then assign that as the src for an image tag. I looked up the API for Google Image search which is ...

  1. display first image in post as thumbnail wordpress

I have a JSON Object with large amount of data. I want to pass an JSON object to the web method that only need to contain the list of one value. Using $.grep we can filter the data like LINQ. But we ...

I have a JSON Object with large amount of data. I want to pass an JSON object to the web method that only need to contain the list of one value. Using $.grep we can filter the data like LINQ. But we ...

  1. selecting item from combobox c#
  2. selecting item from list python
  3. combobox selected item
  4. selecting item in listview c#
  5. selecting item in jquery
  6. selecting item in list
  7. listview selected item
  8. selecting item in javascript
  9. c# selecting item from list
  10. selecting an item from the spinner
  11. selecting a random item from a set