Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Retry on failure
MoyenAsync code10 min
Write a function retry(fn). It calls the async function fn with no arguments. If the promise from fn rejects, call fn again. Make no more than three attempts in total.
retry returns a promise with the result of the first successful attempt. If all three attempts fail, return a promise that rejects with the error of the last attempt.
Exemples
await retry(() => Promise.resolve('ok'))Renvoie 'ok'
await retry(() => Promise.reject(new Error('down'))).catch((e) => e.message)Renvoie 'down'
Chargement de l'éditeur...
- Success on the first try, fn is called once
- Two failures, then success
- Three failures, the last error comes out
Les tests cachés se lancent à la soumission