Async await polling timeout processing

there is a scenario where I need to use setInterval to obtain ajax requests (the backend is unwilling to use webscoket). I find that when the interface is in pending, it is still polling for sending requests. I want to make an optimization, that is, stop sending requests when the interface is pending, and then continue polling until the data is returned at the backend. Can this be solved with async and await?


function sendRequest() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve('receive response');
    }, Math.round(Math.random() * 2000));
  })
}

async function runTasks() {
  for (let i = 0; i < 1000; iPP) {
    const res = await sendRequest();
    console.log(res);
  }
}

runTasks();
This is what

should mean. Wait until the last request is completed and then send the next request. The network request is simulated with setTimeout .


you can set timeout for the ajax request, and resend the request after the timeout.

Menu