1

I would like to know what is the best practice when making http request one after another, especially, I'll be required to use the return value from the first request. Currently, I've a nested subscription to achieve this issue [See the code below].

I tried with siwtchMap, mergeMap & concat from RxJS, but it didn't seem to work. Any suggestion will be help for.

onStartUp() {
    this.recordingService.getRecording(this.id)
     .subscribe(x => {
       this.recording = x;
       const params = new Chunk(this.recording, 0, 30);
       this.recordingService.getSignal(params)
        .subscribe(data => console.log(data));
     });
  }
1
  • What didn't work with switchMap or mergeMap? Commented Jul 3, 2018 at 10:23

1 Answer 1

2

Why switchMap does not work in your case? I think it is the best solution, switchMap receive the result of the stream and return another observable to continue the stream:

onStartUp() {
   this.recordingService.getRecording(this.id)
   .switchMap((x) => {
       this.recording = x;
       const params = new Chunk(this.recording, 0, 30);
       return this.recordingService.getSignal(params);  
   })
   .subscribe(data => console.log(data));
 }

In case you are using pipeable operators:

import { switchMap } from 'rxjs/operators';

this.recordingService.getRecording(this.id)
  .pipe(
      switchMap((x) => {
          this.recording = x;
          const params = new Chunk(this.recording, 0, 30);
          return this.recordingService.getSignal(params);  
      })
  )
  .subscribe(data => console.log(data));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.