How do i know who called RXJS MergeMap response

Viewed 28

So i merge various Observables into one with mergemap, then i subscribe to said merged Observable and i get various responses. But they are anonymous; the response only says "SUCCESS" but i want to know the id that made that response

const requests = from(
    this.array.map((e)=>{
        return this.eventsService.acceptId(e.id);
    })
).pipe(mergeMap( x => x ));

requests.subscribe(
    (res)=>{
        console.log(res)
    }
)

this works as intended but i want to know the id that caused the response inside the subscribe.

the calls need to be parallel, with individual API calls

1 Answers

You can map the ID's in as part of the originoal observables, before merging themall together.

That might look like this:

const requests = merge(
  this.array.map(
    ({id}) => this.eventsService.acceptId(id).pipe(
      map(payload => ({payload, id}))
    )
  )
);

requests.subscribe(
  (res) => {
      console.log(res)
  }
);
Related