BACK
PracticeInterview: exhaustMap() double-submit protection
PRACTICE · 30 · 練

Interview: exhaustMap() double-submit protection

Two clicks arrive synchronously while the request is async. With exhaustMap, only the first should get through.

  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 { of, timer, exhaustMap, map } = Rx;

const request = id => timer(0).pipe(map(() => 'done-' + id));

const result$ = of(1, 2).pipe(
  exhaustMap(id => request(id))
);

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