Recursive Http Calls. The RXJS way ?

This article will show a quick example of achieving recursive http calls using RXJS for those who suffer from Promises.

Imagine that you want to collect all entries from an endpoint that only supports a paging API, means that you can’t get all once. S…


This content originally appeared on DEV Community and was authored by Fateh Mohamed ?

This article will show a quick example of achieving recursive http calls using RXJS for those who suffer from Promises.

Imagine that you want to collect all entries from an endpoint that only supports a paging API, means that you can't get all once. So the idea is to keep calling the Api by page till the last page which is our condition which our stop condition.

I'll use an Endpoint to fetch Artists

image

While next is truthy we proceed to the next call with the new page.

Let's move to the fun stuffs ?, To achieve that we will have a magic rxjs operator EXPAND.

import { from, EMPTY } from 'rxjs';
import { expand, tap, switchMap } from 'rxjs/operators';

let rows = []
const firstUrl = 'https://api.happi.dev/v1/artists?page=1'
const getArtists = (url: string) => from(fetch(url)).pipe(
    switchMap(response => response.json())
)

getArtists(firstUrl).pipe(
 tap(response => rows = rows.concat(response.result)), // on success we concat with the new coming rows
 expand((previousData) => previousData.next 
            ? getArtists(previousData.next)
            : EMPTY;
 )
).subscribe();

Alt Text

That's all ?, Goodbye ??


This content originally appeared on DEV Community and was authored by Fateh Mohamed ?


Print Share Comment Cite Upload Translate Updates
APA

Fateh Mohamed ? | Sciencx (2021-04-26T09:09:53+00:00) Recursive Http Calls. The RXJS way ?. Retrieved from https://www.scien.cx/2021/04/26/recursive-http-calls-the-rxjs-way-%f0%9f%98%8e/

MLA
" » Recursive Http Calls. The RXJS way ?." Fateh Mohamed ? | Sciencx - Monday April 26, 2021, https://www.scien.cx/2021/04/26/recursive-http-calls-the-rxjs-way-%f0%9f%98%8e/
HARVARD
Fateh Mohamed ? | Sciencx Monday April 26, 2021 » Recursive Http Calls. The RXJS way ?., viewed ,<https://www.scien.cx/2021/04/26/recursive-http-calls-the-rxjs-way-%f0%9f%98%8e/>
VANCOUVER
Fateh Mohamed ? | Sciencx - » Recursive Http Calls. The RXJS way ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/26/recursive-http-calls-the-rxjs-way-%f0%9f%98%8e/
CHICAGO
" » Recursive Http Calls. The RXJS way ?." Fateh Mohamed ? | Sciencx - Accessed . https://www.scien.cx/2021/04/26/recursive-http-calls-the-rxjs-way-%f0%9f%98%8e/
IEEE
" » Recursive Http Calls. The RXJS way ?." Fateh Mohamed ? | Sciencx [Online]. Available: https://www.scien.cx/2021/04/26/recursive-http-calls-the-rxjs-way-%f0%9f%98%8e/. [Accessed: ]
rf:citation
» Recursive Http Calls. The RXJS way ? | Fateh Mohamed ? | Sciencx | https://www.scien.cx/2021/04/26/recursive-http-calls-the-rxjs-way-%f0%9f%98%8e/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.