通过http将一个forkJoin的结果传递给另一个的最佳方法

问题描述 投票:0回答:2

我需要进行多个http调用(角度),并将一个forkJoin的结果返回给另一个。以下将不起作用:

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;
      })
}

因为res未定义。是否可以使用forkJoin运行顺序的HTTP调用并将一个HTTP调用的结果传递给下一个?

谢谢

angular rxjs rxjs6 fork-join
2个回答
2
投票

我不确定我是否正确解释了您的问题,但是您可以考虑使用RxJS switchMap运算符。 switchMap允许您将createAccountHttp中的可观察值映射到内部可观察值。

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

-1
投票

我的意思是这样

async function() {
const res = await this.userService.createAccountHttp(accountRequest).toPromise();
const res2 = await this.userService.verifyAccountHttp(res).toPromise();

}
© www.soinside.com 2019 - 2024. All rights reserved.