Observable of array to array (Rxjs)

thk

I have a service and its getAllMessages() returns an observable of an array of items : Observable<ITEM[]>

I map the result like this to post each ITEM to a RestAPI :

this.service.getAllMessages()
.map(items => ...for each item of items, call this.apiService.postMessage(item)...)

postMessage(item) returns an Observable<Response>

My goal is to get an Observable<Response[]> for which I can check the http code of every post from this.apiService.postMessage(item)

I tried using flatMap :

this.service.getAllMessages()
  .flatMap(items => {
    return items.map(item => {
      return this.apiService.postMessage(item);
    });
  }).subscribe(RES => {
    console.log(RES);
  });

but here RES is an Observable<Response> and not a simple Response

How can I achieve this ?

CozyAzure

What is returned by your this.service.getAllMessages() is an array of Observable, not an Observable of array. The .map() function belongs to the function of array.

If you want to execute all the Observables in parallel, use .forkJoin():

this.service.getAllMessages()
    .flatMap(items => {
        return Observable.forkJoin(items.map(item => this.apiService.postMessage(item)));
    })
    .subscribe(RES => {
        console.log(RES);
    });

If you want to execute the observables in sequential, use .concat():

this.service.getAllMessages()
    .flatMap(items => {
        return Observable.concat(...items.map(item => this.apiService.postMessage(item)));
    })
    .subscribe(RES => {
        console.log(RES);
    });

Note the spread operator for .concat.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

RxJs Array of Observable to Array

RxJs Observable in Observable from Array

Simple filter on array of RXJS Observable

rxjs Filter an array of objects observable

RxJs Observable Zip Array of Observables

RxJS observable of array not working as expected

RxJS - Test for empty Observable array

RxJS/Observable flatMap can return Observable or array

RxJS append observable to observable array list in typescript

How to turn an observable of an array into an array of observables (RxJS)

Angular - rxjs - Transform Observable array to another Observable array

How to merge Array Observavble of array in only one Observable of array with RXJS?

RxJS 6 filter and map an observable array of items

Angular RxJS Observable Filter on array of objects

RxJs how to map the items in an Observable<Array>

Observable Array of Observables - How to implement in RxJS?

Best way to "flatten" an array inside an RxJS Observable

How to add item in rxjs observable array in angular

rxjs6 filter Observable<Array<any>>

RxJS: chunk and delay on a large observable array

Observable of array with filter using Angular 5 with RxJS

RXJS - Multiple observable calls on array of properties

Rxjs Observable run sequentially and return an array

RXJS Observable Transform Array To Multiple Values

RxJS Observable: Emit every specific change in the array

How to push to Observable of Array in Angular 4? RxJS

RxJS 6 get a filtered list of array of Observable

RxJS groupBy on ngrx effects to observable custom array

Modify Observable array before subscribing by rxjs operators