lloydaf
lloydaf

Reputation: 605

Access to observable in Rxjs Forkjoin

So I have a list of observables on which I am applying the forkJoin operation. I want to know which observable each object in the responses array refers to.

let observables = ...//array of observables
forkJoin(observables).subscribe(responses=>{
  responses.forEach(response=>{
    //figure out what observable this response corresponds to
  });
});

So basically this is for using one of the request params after I receive the http response. Ideally I can modify the response from the server and pass some attribute in the response. But I don't have that option currently. Any ideas on how I can access the request object after subscribing to the response?

Upvotes: 0

Views: 872

Answers (2)

Suresh Kumar Ariya
Suresh Kumar Ariya

Reputation: 9784

You can also try like this.

let observables = [of(1), of(2), of(3)];

forkJoin(observables)
    .subscribe(
        ([typeData1, typeData2, typeData3]) => {
            // typeData1 => 1st observable result
            // typeData2 => 2nd observable result
            // typeData3 => 3rd observable result
            this.isLoaded = true;
        }
    );

For Dynamic observable array,

let observables = [of(1), of(2), of(3), of(4)];

forkJoin(observables)
  .subscribe(
    ([...typeDataArr]) => {
        console.log(typeDataArr);
    }
  );

Upvotes: 1

Xinan
Xinan

Reputation: 3162

let observables = getMyObservables();

forkJoin(observables).subscribe(responses=>{
    responses.forEach(response=>{
       //figure out what observable this response corresponds to
       console.log(response.someKindOfIdentifier);
    });
});


getMyObservables() {
  return this.http.get('blah_blah_blah').pipe(map(response => {
    originalResponse: response,
    someKindOfIdentifier: WHATEVER_YOU_WANT
  });
}

Upvotes: 0

Related Questions