Skip to content
Advertisement

Need to do API calls on Array of data and get the response Angular

I want to implement a logic where if there is no network connectivity then i am storing the data in frontend in local storage and whenever got connected to network then I want to do api call on the this array of data from local storage. And if call is success then remove this item from storage and continue the process until done.

I have tried many ways using promise.all and forkJoin. Can anybody suggest the proper way to do it.

Advertisement

Answer

Here is the solution for the above question:

 makeHttpCalls(elements: any[]): Observable<any[]> {
if (this.network.type === 'none') {
  console.log('No network connection');
  return of(null);
}
const requests = elements.map(element => this.http.post(element.url, element.data));
return forkJoin(requests).pipe(
  map(responses => {
    return elements.filter((element, index) => !responses[index].success);
  }),
  catchError(error => {
    console.log(error);
    return of(null);
  })
);

} }

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement