Retry with exponential backoff

Retry idempotent asynchronous work using bounded exponential delays.

Original Pearto instructional example. Test it in your own environment and adapt it to your requirements.

News & updates

No news stories have been linked to this profile.

Search news coverage ↗

Sources & useful links

No publisher or independent source links have been added to this entry.

Suggest a source or correction ↗
async function retry(task, attempts=3) {
  for (let i=0; i<attempts; i++) {
    try { return await task(); }
    catch (err) {
      if (i === attempts-1) throw err;
      await new Promise(resolve => setTimeout(resolve, 200 * 2**i));
    }
  }
}
// Retry only operations that are safe to repeat.