Language concepts

Retry with backoff

Try the free JavaScript compiler

Retry policy increasing wait between attempts, typically bounded and optionally randomized. Retry only classified transient failures and operations safe to repeat; inject timing dependencies for tests.

Formal Syntax & Structure

javascript syntaxjavascript
await wait(baseDelay * 2 ** attempt);

Working Example

Example Snippetjavascript
async function retry(task, attempts, wait) {  if (!Number.isInteger(attempts) || attempts < 1) throw new RangeError("attempts");  for (let i=0;i<attempts;i++) {    try { return await task(); } catch(e) {      if (!e.transient || i===attempts-1) throw e;      await wait(2**i);    }  }}let count=0; retry(async()=>{if(++count<3) throw {transient:true}; return "ok";},3,async n=>console.log("wait",n)).then(v=>console.log(v,count));
JavaScript Reference