'Is it possible to make await + setTimeout + for loop work together?

I use async and for loop to test for fetch issues that might be caused by network or other issues. but testing too often It generates a request error similar to endless pings. After returning, I want to wait 3000 milliseconds before restarting the test. This is my code:

 const fetchtest = async (url, opts, tries=FETCHTEST_LOOP) => {
  const sleep = m => new Promise(r => setTimeout(r, m));
  const errs = [];

  for (let i = 0; i < tries; i++) {

    try {
      return await fetch(url, opts).then(sleep(3000)) ; // << When returned, wait 3000 ms to restart the test.
    }
    catch (err) {
      errs.push(err);
    }

}

  throw errs;
};

this is my log:

𓊈2𓊉 369 update at actiVity
trying GET [1 of 10]
trying GET [2 of 10]
trying GET [2 of 10]
trying GET [1 of 10]
trying GET [2 of 10]
trying GET [1 of 10]
trying GET [2 of 10]
trying GET [4 of 10]
trying GET [3 of 10]
trying GET [2 of 10]
trying GET [1 of 10]
trying GET [3 of 10]
trying GET [1 of 10]
trying GET [2 of 10]
trying GET [1 of 10]
trying GET [2 of 10]
trying GET [4 of 10]
trying GET [3 of 10]
trying GET [1 of 10]
trying GET [2 of 10]
trying GET [2 of 10]
trying GET [2 of 10]
trying GET [1 of 10]
trying GET [3 of 10]
trying GET [2 of 10]
trying GET [1 of 10]
trying GET [4 of 10]
trying GET [2 of 10]
trying GET [1 of 10]
trying GET [2 of 10]
trying GET [1 of 10]
𓊈2𓊉 370 update at actiVity
trying GET [1 of 10]
trying GET [2 of 10]
𓊈2𓊉 370 update at actiVity
𓊈2𓊉 370 update at actiVity

I am new to this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source