born2net
born2net

Reputation: 24953

best way to pass results of one forkJoin to another via http

I need to make multiple http calls (angular) and return the results of one forkJoin to another. the following will not work:

Observable.forkJoin([
       this.userService.createAccountHttp(accountRequest),
       this.userService.verifyAccountHttp(res)
 ]);

createAccount() {
  return this._http.jsonp<any[]>('http://...', 'callback').pipe(
      map(v => {
        return v;
      })
}
verifyAccountHttp(res) {
  return this._http.jsonp<any[]>('http://some url/' + res, 'callback').pipe(
      map(v => {
        return v;
      })
}

as res is undefined. Is there a way to use forkJoin to run sequential http calls and pass results of one http call to the next?

Thanks

Upvotes: 0

Views: 1967

Answers (1)

wentjun
wentjun

Reputation: 42526

I am not sure if I have interpreted your question correctly, but the RxJS switchMap operator might be something you can consider. switchMap will allows you to map over the observable values from createAccountHttp into the inner observable.

this.userService.createAccountHttp(accountRequest)
  .pipe(
    switchMap((res) => this.userService.verifyAccountHttp(res)),
  ).subscribe((res) => {
    // do the rest here
  });

Upvotes: 2

Related Questions