I have two api endpoinst, the first one for fetching the list and second one for fetching the details by id. Now, I have to conbine these two togethere to get a list with details information.
For now, I used forkJoin to do this, and it works perfect except the request is send in parallel.
But I can't send so many request in parallel since the api server has rate-limit, how can I send request in series and get the same result?
/* get list
[
{id: 1, name: 1},
{id: 2, name: 2},
{id: 3, name: 3}
]
*/
function getList() {
return get('http://example.com/list')
}
/* get details by id
{
id: 1,
author: 'hello',
title: 'world'
}
*/
function getDetails(id) {
return get('http://example.com/list/' + id)
}
// now this works, but all request send in parallel
Observable.fromPromise(getList()).switchMap(data =>
Observable.forkJoin(data.map(item =>
Observable.fromPromise(getDetails(item.id)).map(details => ({
...item,
details
}))
))
).map(data => console.log(data))
//data is an array here
/*
[
{id: 1, name: 1, details: {id:1, author: 'hello', title: 'world'}},
{id: 2, name: 2, details: ...},
{id: 3, name: 3, details: ...}
]
*/