Skip to content

Instantly share code, notes, and snippets.

@Stuff90
Last active May 7, 2022 02:53
Show Gist options
  • Save Stuff90/08378cd369bb7b05ea16170dda7a2cd0 to your computer and use it in GitHub Desktop.
Save Stuff90/08378cd369bb7b05ea16170dda7a2cd0 to your computer and use it in GitHub Desktop.
Example of a recursive call to an API using Observables
// 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