Backoff Decorator For Async Methods
Apply exponential backoff retry to async methods via a decorator with per-method configuration and error filtering.
Code
export function withBackoff({maxAttempts=3,baseDelay=100,shouldRetry=()=>true}={}){return function(target,propertyKey,descriptor){const original=descriptor.value;descriptor.value=async function(...args){let attempt=0,lastErr;while(attempt<maxAttempts){try{return await original.apply(this,args);}catch(e){lastErr=e;attempt++;if(attempt>=maxAttempts||!shouldRetry(e))break;await new Promise(r=>setTimeout(r,baseDelay*2**attempt));}}throw lastErr;};return descriptor;};}Report a problem with this exampleOpens a pre-filled issue on GitHub.