1

I'd like to make recursive HTTP calls. I understand that I can make the initial call and then use pipe and expand to repeat the HTTP call.

However, as shown in the pseudo-code below, the callback returns the parsed response so the flow is asynchronous. In this scenario, how do I pipe the parsed response?

const fetchPage = (request) => {
  return ajax(request).pipe(map(response => {
      if (response.status == 200) {
          // The callback returns the parsed response, how can/should I wait for the callback to return?
          parser.parse(response.responseText, callback);
      } else {
          return EMPTY;
      }
  }));
}

fetchPage(request).pipe(
expand(parsedResponse => {
  if (checkLast(parsedResponse)) {
    return EMPTY;
  } else {
    return fetchPage(parsedResponse.next);
  };
}),
concatMap(parsedResponse => parsedResponse));
Ari
  • 4,121
  • 8
  • 40
  • 56
  • Possible duplicate of [RxJs Observable Pagination](https://stackoverflow.com/questions/35254323/rxjs-observable-pagination) – Oles Savluk Mar 27 '19 at 20:00

1 Answers1

0

You should do parsing before you expand it.

E.g.

const fetchPageParsed = () => {
  return fetchPage().pipe(
    map(response => parse(response))
  );
};

fetchPageParsed().pipe(
  expand(parsed => {
    if (parsed.last) {
      return EMPTY;
    }

    return fetchPageParsed(parsed.nextPage);
  })
)
kos
  • 5,044
  • 1
  • 17
  • 35