Skip to content

Instantly share code, notes, and snippets.

@Kapcash
Created September 27, 2018 12:47
Show Gist options
  • Save Kapcash/b316bb7575090f1deedf0465cb73e812 to your computer and use it in GitHub Desktop.
Save Kapcash/b316bb7575090f1deedf0465cb73e812 to your computer and use it in GitHub Desktop.
Recursive observable sequence, depending on previous response and concatening results
repeatRequests(req, endpoint, callback): any{
// Array of all results, returned to the subscribe
let tempArray = [];
// Recursive lambda function to chain all observables sequentially
const recursive = (req, endpoint, callback) => {
return this.httpService.get(endpoint).pipe(
flatMap((res) => {
callback(res.data.items);
// Concat all results in the result array
tempArray = tempArray.concat(res.data.items);
if(res.data.next) {
// Run the next call if needed
return recursive(req, res.data.next, callback);
} else {
// It's the last call, so we return the full array with all calls results
return of(tempArray);
}
})
);
}
return recursive(req, endpoint, callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment