【问题标题】:Using RXJS to issue two sequential http calls and returning the result of the first one only使用 RXJS 发出两个连续的 http 调用并仅返回第一个的结果
【发布时间】:2018-09-25 23:34:29
【问题描述】:

我将 RXJS 6 与 Angular 6 HttpClient 一起使用,我正在尝试按顺序执行两个 http 调用。

  • 我需要只返回第一次调用的结果
  • 第一个调用必须在第二个调用之前发出

我想到了使用tap方法如下:

someMethod(items: Item[]): Observable<Item[]> {
   const one = this.http.put<{ items: Item[] }>('urlOne', items);
   const two = this.http.put<void>('urlTwo', items);

   return one.pipe(
     mergeMap((res)=> res.items),
     tap(two)
   );
}

有没有更好的方法?如果有怎么办?

【问题讨论】:

    标签: angular rxjs angular-httpclient rxjs6


    【解决方案1】:

    您可以使用map 并忽略第二个结果:

    one.pipe(
      mergeMap((res) => two.pipe(
        mapTo(res)
      ),
    );
    

    【讨论】:

    • 非常感谢 :-)
    猜你喜欢
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多