BACK
PracticeReal-life: expand() cursor pagination
PRACTICE · 51 · 練

Real-life: expand() cursor pagination

With expand, load pages 1 -> 2 and flatten the items into a stream.

  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, EMPTY, from, expand, mergeMap, map } = Rx;

const pages = {
  1: { items: ['a'], next: 2 },
  2: { items: ['b'], next: null },
};

const result$ = of(1).pipe(
  expand(page => pages[page].next ? of(pages[page].next) : EMPTY),
  mergeMap(page => from(pages[page].items))
);

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