Last active
May 7, 2022 02:53
-
-
Save Stuff90/08378cd369bb7b05ea16170dda7a2cd0 to your computer and use it in GitHub Desktop.
Example of a recursive call to an API using Observables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example for article : https://medium.com/@simonb90/appels-api-itératifs-avec-rxjs-a1c2593c558b | |
/* | |
* Definition of API method: | |
* getEntitiesByPage(page: number): Observable<Entity[]> | |
*/ | |
let iterator = new BehaviorSubject<number>(1); | |
iterator.mergeMap((i) => { | |
return getEntitiesByPage(i).do((entities) => { | |
if (entities.length > 0) { | |
iterator.next(i + 1); | |
} | |
}); | |
}) | |
.takeWhile(entities => entities.length > 0) | |
.startWith([]) | |
.scan((allEntities, currentPageEntities) => | |
allEntities.concat(currentPageEntities) | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment