In my current function, I am receiving an array of objects called data/ids as a parameter. Within this function, I need to execute a post request for each element/id:
fillProfile(users) {
const requests = [];
console.log( 'USERS.length:', users.length );
requests.push( this.http.post( '/test/add', users[i] ) );
for ( let i = 0; i < users.length; i++) {
return this.http.post( '/test/add', users[i] ).pipe(
map( (res) => {
console.log( 'res: ', res );
return res;
}),
catchError(err => {
return throwError(err);
})
);
}
Although my current implementation works, it is not considered a good practice. From my research so far, I have found that this can be achieved using forkJoin or other operators. I have attempted some solutions mentioned on Stack Overflow but none of them seem to work.
One approach I tried which was unsuccessful:
....
return from(requests2).pipe(
concatMap((request) => request.pipe(
delay(500),
map( res => {
// console.log( 'res: ', res );
return res;
})
))
);
Do you have any suggestions on how to improve the execution of multiple requests, and also handle responses and errors of each one separately in a more efficient manner?