I need to invoke a web service for each of the item in the list ["A", "B", "C"]. The service will return data in pages (default page size 100). The first invocation of service(with page = 1 as parameter) will have total number of records set as a header in response and based on that I need to invoke the service multiple times. And I need to wait for response of all the invocations, to proceed with further logic. Any pointers to implement the above using Rxjs will be of great help
Asked
Active
Viewed 56 times
-1
-
https://stackoverflow.com/questions/35254323/rxjs-observable-pagination is this is what you are looking for? – Oles Savluk Aug 31 '17 at 07:12
-
Hi. I got it working for my requirement, but the response is not in correct format. The response from each of the service call is an array of objects. So I think in the final stream should be having multiple array objects ( like [{},{}] []{},{} [{},{}]), But I am getting a stream of flattened objects. (like {}{}{}) – Anil Bhaskaran Sep 01 '17 at 05:53
1 Answers
0
Using the expand
operator you can recursively invoke your api (getData(x)
) to retrieve paged data until you have filled your needs (simulated with a .take(7)
)
function getData(page) {
return Rx.Observable.of({
page: page,
hasMoreData: page !== 4,
data: [
page * 10 + 1,
page * 10 + 2,
page * 10 + 3,
page * 10 + 4,
page * 10 + 5
]
})
.delay(1000)
.do(() => console.log('retrieved data for page: ' + page));
}
getData(1)
.expand(data => {
if (data.hasMoreData) {
return getData(data.page + 1);
}
return Rx.Observable.empty();
})
.mergeMap(dataObj => dataObj.data)
.take(7)
.subscribe(val => console.log('data: ' + val));
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.2/Rx.js"></script>

Mark van Straten
- 9,287
- 3
- 38
- 57