Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
Chaining subscription to multiple observables with parameter

I would like to subscribe two observables one after the other. The order is important and must be kept. The first observable returns a result itemId which must be passed to the second subscription. Currently, I use nested subscriptions, which is not very nice. What is the cleanest way to implement this?

// 1
this.widget$
  .subscribe((widget) => {
    const itemId: number = widget.data[0].itemId;

    // 2
    this.store
      .select(DeviceHistoryStore.getItemHistoryEntries(this.deviceId, itemId))
      .subscribe((deviceHistory) => {
        const name = widget.name; 
        // Run code
    });
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Simply use a SwitchMap.

this.widget$.pipe(
    switchMap(widget => 
        this.store
            .select(DeviceHistoryStore.getItemHistoryEntries(
                this.deviceId, 
                widget.data[0].itemId
            ))
    )
).subscribe(deviceHistory => { /* ... */ )

Edit:

If you want to access widget in the subscribe callback:

this.widget$.pipe(
    switchMap(widget => 
        combineLatest([
            of(widget),
            this.store
                .select(DeviceHistoryStore.getItemHistoryEntries(
                    this.deviceId, 
                    widget.data[0].itemId
                ))
        ])        
    )
).subscribe(([widget, deviceHistory]) => { /* ... */ )
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!