如何使用RxJS执行并获得顺序HTTP调用的结果 [英] How to execute and get result of sequential HTTP calls using RxJS

查看:76
本文介绍了如何使用RxJS执行并获得顺序HTTP调用的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想依次进行几个HTTP调用,并且在每个调用之后,我想检查结果并进行另一个调用并显示消息,等等.在某些情况下,继续进行下一个调用,在某些情况下,在显示消息后中断该调用.另一方面,我还需要在每个调用中使用 catchError 块(或者对每个调用都使用一个块,但对每个调用使用一个块会更好).

I want to make several HTTP calls sequentially and after each call, I want to examine the result and make another call and display message, etc. In some cases continue to the next call, some cases break on that call after displaying message. On the other hand, I also need to use catchError block of each call (or maybe a single block for all of them, but using one for each call would be better).

因此,假设我有一个创建,更新和删除呼叫,如下所示.如何获得这些呼叫的每个响应并检查响应,然后执行另一个呼叫?

So, assume that I have an create, update and delete calls as shown below. How can I get each of these call's response and check the response and then execute another call?

我已经尝试过 switchMap iif ,并且我正在寻找使用其中一种的解决方案.有什么建议吗?

I have tried switchMap and iif and I am looking a solution using one of them. Any suggestion?

this.service.create('/api/employee').subscribe(resp1 => {
    this.service.update('/api/salary/', id).subscribe(resp2 => {
        this.service.delete('/api/education/', id).subscribe(resp3 => {

        // I need to get response of each call from starting thr first and then continue of 
        // break according to the condition of the response

  });
});

推荐答案

我不确定这里还有很多话要说.

I'm not sure there's much else to say here.

值得注意的是, EMPTY 是一个RxJs流,它不发出任何内容并立即完成.这有点像中断".(以一种非常宽松的方式.您如何从流中中断"取决于上下文).

Of note, perhaps, is that EMPTY is an RxJs stream that emits nothing and completes immediately. It is sort of like "break" (in a pretty loose way. How you "break" from a stream can be pretty context dependant).

this.service.create('/api/employee').pipe(
  catchError(err1 => {
    // do some stuff
    return throwError(err1);
  }),
  switchMap(resp1 => {
    // do some stuffs
    if(someCondition(resp1)){ 
      return this.service.update('/api/salary/', id).pipe(
        catchError(err2 => {
          // do some stuff
          return throwError(err2);
        }),
      );
    }
    return EMPTY;
  }),
  switchMap(resp2 => {
    // do some stuff
    if(someCondition(resp2)){ 
      return this.service.delete('/api/education/', id).pipe(
        catchError(err3 => {
          // do some stuff
          return throwError(err3);
        }),
      );
    }
    return EMPTY;
  }),
).subscribe({
  next: resp3 => { /*do some stuff*/ },
  complete: () => { /*Your stream is done*/ },
  eror: err => { /*All re-thrown errors end up here */ }
});

更新

使用 tap 帮助理解流

tap 是一个运算符,它返回接收到的相同流.它无法对您的信息流进行任何更改,但可以查看在任何给定时间点正在发生的情况.这可能是有助于您理解的有用工具.

Update

Using tap to help understand streams

tap is an operator that returns the same stream that it receives. It can't make any changes to your stream, but it can look at what is happening at any given point. This can be a useful tool to aid your understanding.

http1().pipe(
  tap({
    next: val => console.log("Result from http1 call: ", val),
    complete: () => console.log("http1 call completed"),
    error: err => console.log("http1 call errored: ", err)
  })
  switchMap(val => http2(val).pipe(
    tap({
      next: val => console.log("Result from http2 call: ", val),
      complete: () => console.log("http2 completed"),
      error: err => console.log("http2 call errored: ", err)
    })
  )),
  tap({
    next: val => console.log("Result from switchMap operator: ", val),
    complete: () => console.log("switchMap completed"),
    error: err => console.log("switchMap (http1 or http2 call) errored: ", err)
  })
).subscribe()

在这里,我们可以看到 switchMap 之前和之后发生了什么.您可以看到在这种情况下,switchMap从http1获取一个值,并从http2发出该值.

Here we can see what is happening before and after the switchMap. You can see that in this case, switchMap is taking a value from http1 and emitting the values from http2.

因为switchMap在生成http2之前等待http1的值,所以副作用是http2直到http1发出后才开始.这确实意味着这些调用是按顺序执行的,但是如果http1发出多次以上,故事就会变得更加复杂.

Because switchMap waits for values from http1 before generating http2, one side effect is that http2 doesn't start until after http1 emits. This does mean that these calls are executed sequentially, but the story gets more complicated if http1 emits more than once.

这篇关于如何使用RxJS执行并获得顺序HTTP调用的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆