Created
September 27, 2018 12:47
-
-
Save Kapcash/b316bb7575090f1deedf0465cb73e812 to your computer and use it in GitHub Desktop.
Recursive observable sequence, depending on previous response and concatening results
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
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