Proof of Work · Challenge
Call again, wait longer each time, then give up honestly.
about 30 min · 7 required tests · in the browser
What is recorded: when you save, your Result page shows your code, which tests passed, how many times you ran the tests, how many versions you tried, the time from first run to save, and the largest jump in code between runs. Anyone with the link can see it. The suggested time is not enforced.
A request to a flaky service fails now and then. Retrying right away usually fails again, so you wait a little, then longer, then longer still. That is exponential backoff.
Implement retry(fn, options). It calls the async function fn and returns a promise.
options.maxAttempts is the total number of calls to fn, including the first. It defaults to 3.options.baseDelay is the wait in milliseconds after the first failure. It defaults to 100.baseDelay, then twice that, then four times, and so on.fn receives the attempt number, starting at 1.
With maxAttempts of 3 and baseDelay of 100:
const data = await retry((attempt) => fetchProfile(attempt))
// attempt 1 fails, wait 100ms
// attempt 2 fails, wait 200ms
// attempt 3 fails, reject with attempt 3's error. No wait after it.The grader moves time on a virtual clock. setTimeout and clearTimeout work. Time does not pass on its own, and setInterval is not available.
Requirements:
maxAttempts calls and reject with the last error.fn.Write a top-level function named retry. Do not use export or module.exports.
Press Esc, then Tab, to leave the editor.
⌘ / Ctrl + Enter runs the tests
Hiring developers? See how teams use this.
Graded on
0/7
Other builds