BACK
PracticeInterview: retry({ delay })
PRACTICE · 59 · 練

Interview: retry({ delay })

Retry a flaky API with a retry config and a delay via timer(0).

  1. Solve the task in the editor — no long theory.
  2. Run the check and compare your output with the expected result.
  3. Stuck? Open the solution and carry the approach into your own code.
Solution spoiler · click to reveal
const { defer, throwError, of, retry, timer } = Rx;

let attempt = 0;
const api$ = defer(() => {
  attempt += 1;
  return attempt < 3 ? throwError(() => new Error('fail')) : of('ok-' + attempt);
});

const result$ = api$.pipe(
  retry({ count: 2, delay: () => timer(0) })
);

result$.subscribe(value => console.log(value));
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...