Skip to main content

Promise Retry

MIDDLE

Promise Retry

Create a function that retries an async operation a specified number of times.

Requirements:

  • Implement retry that takes an async function and retry count
  • Retry the function if it fails, up to the specified number of times
  • If all attempts fail, reject with the last error

Example:

let attempt = 0;
async function unreliableFunction() {
  attempt++;
  if (attempt < 3) throw new Error('Failed');
  return 'Success';
}

retry(unreliableFunction, 3).then(result => {
  console.log(result); // 'Success'
});

Examples:

Input 1:{"retries":3}
Output 1:"success"

Loading editor...

Run your code to see results

Click the Run button above