BACK
PracticeInterview: retry() flaky API
PRACTICE · 32 · 練

Interview: retry() flaky API

The source fails the first two attempts. With retry(2), get ok-3.

  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 } = Rx;

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

const result$ = api$.pipe(
  retry(2)
);

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