Skip to content

Instantly share code, notes, and snippets.

@marcusdb
Forked from Stuff90/observable-recursive.js
Created June 5, 2018 09:39
Show Gist options
  • Select an option

  • Save marcusdb/dfdb6756b28d94be9250d2c221f383ee to your computer and use it in GitHub Desktop.

Select an option

Save marcusdb/dfdb6756b28d94be9250d2c221f383ee 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