Having a service method for API calls structured as follows:
getUsers(id){
return this.http.get(`${env.apiURL}/id`)
}
Now, the requirement is to call this method for a list of users stored in an array:
userId=[1,2,3,4,5,6,7,8,9]
The goal is to retrieve and print results from all API calls by utilizing fork-join as shown below:
let user1= this.http.get(baseurl+'users/userId[1]');
let user2= this.http.get(baseurl+'users/userId[2]');//Similarly, there are 10 values
forkJoin([user1, user2]).subscribe(results => {
// results[0] corresponds to user1
// results[1] corresponds to user2
});
However, it was noticed that the API calls were executed in parallel rather than sequentially. The ideal scenario requires sequential (synchronous) API calls.
Is there a more efficient way to make these n (variable number of users) API calls sequentially?
Note: There's also a need to introduce a delay of 500ms after each API call. Attempts with pipe(throttleTime(500))
following the forkJoin
operation resulted in simultaneous execution of all API calls.