Я бы использовал Rx Js и Observable , чтобы сделать это элегантным способом, например
// Emit people ids as a sequence
const source = Rx.Observable.from([1,2,3,4,5,6,7,8,9]);
const subscribe = source
.bufferCount(5) // Buffer people ids to ensure the size won't exceed 5 for api calls
.flatMap(peopleIds => peopleApi.getPeople(peopleIds))
.map(people => people.map(user => user.policyId))
.flatMap(policyIds => policyApi.getPolicyDetails(policyIds))
.subscribe(policies => console.log(policies));
Вот фрагмент, высмеивающий ваш API
const mockPeople = [
{ "id": 1, "name": "Jack", "policyId": "p-01" },
{ "id": 2, "name": "Isobel", "policyId": "p-02" },
{ "id": 3, "name": "Steve", "policyId": "p-03" },
{ "id": 4, "name": "James", "policyId": "p-04" },
{ "id": 5, "name": "Marty", "policyId": "p-05" },
{ "id": 6, "name": "Janis", "policyId": "p-06" },
{ "id": 7, "name": "Annabel", "policyId": "p-07" },
{ "id": 8, "name": "Flora", "policyId": "p-08" },
{ "id": 9, "name": "Richard", "policyId": "p-09" },
]
const mockPolicies = [
{ "id": "p-01", "details": "Details for Jack's policy" },
{ "id": "p-02", "details": "Details for Isobel's policy" },
{ "id": "p-03", "details": "Details for Steve's policy" },
{ "id": "p-04", "details": "Details for James's policy" },
{ "id": "p-05", "details": "Details for Marty's policy" },
{ "id": "p-06", "details": "Details for Janis's policy" },
{ "id": "p-07", "details": "Details for Annabel's policy" },
{ "id": "p-08", "details": "Details for Flora's policy" },
{ "id": "p-09", "details": "Details for Richard's policy" }
]
let mockGetPeople = async (peopleIds) => {
let filteredPeople = mockPeople.filter(user => {
return peopleIds.indexOf(user.id) !== -1;
});
return Promise.resolve(filteredPeople);
}
let mockGetPolicies = async (policyIds) => {
let filteredPolicies = mockPolicies.filter(policy => {
return policyIds.indexOf(policy.id) !== -1;
});
return Promise.resolve(filteredPolicies);
}
const source = Rx.Observable.from([1,2,3,4,5,6,7,8,9]);
const subscribe = source
.bufferCount(5)
.flatMap(peopleIds => mockGetPeople(peopleIds)) // mock of peopleApi.getPeople
.map(people => people.map(user => user.policyId))
.flatMap(policyIds => mockGetPolicies(policyIds)) // mock of policyApi.getPolicyDetails
.subscribe(policies => console.log(policies));
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.8/dist/global/Rx.umd.js"></script>