Angular-多个条件HTTP请求 [英] Angular - Multiple conditional http requests

查看:63
本文介绍了Angular-多个条件HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据每个请求的结果,我正在努力提出一个很好的解决方案来处理多个http请求.到目前为止,只要switchMap只有2个http请求,我一直在做得很好.但是,我发现我需要执行3,并且我不确定如何使用switchMap进行操作,或者不确定是否有更好的解决方案.

I'm struggling a bit with coming up with a good solution for doing multiple http requests, based on the result of each of them. So far, I've been doing good with switchMap, as long as it's only 2 http requests. However, I've found out I need to perform 3, and I'm not sure how to do it with switchMap, or if there's a better solution for it.

基本上,我将等待响应,如果返回为空,则我要执行第二个请求,如果返回为空,则我要执行第三个请求.

Basically, I will await a response, and if it comes back empty, I want to perform a 2nd request, and if that comes back empty, I want to perform a 3rd request.

让我尝试给出到目前为止所得到的结果的简化示例:

Let me try to give a simplified example of what I've gotten so far:

getData(): Observable<number> {
  const $subject = new Subject<number>();

  // loadData() takes in an id to specify what data to load
  service.loadData(1).pipe(
    switchMap(response => {
      if(response.length === 0) {
        return service.loadData(2);
      }
      else {
        return of(response);
      }
    }),
  ).subscribe(result => {
    $subject.next(result);
    $subject.complete();
  });
  return $subject.asObservable();

现在这很好用,我的问题是,调用"loadData(2)"可能还会返回长度为0的响应,在这种情况下,我需要调用loadData(3).

Now this works just fine, my issue is, that calling "loadData(2)", might also give back a response with length = 0, and in that case, I need to call loadData(3).

我尝试在switchMap中包含另一个switchMap,但这似乎无法正常工作.所以我的问题是,我如何能够检查loadData(2)的响应长度,如果为0,则返回service.loadData(3)?

I tried having another switchMap within the switchMap, but that didn't seem to work properly. So my question is, how would I be able to check the length of the response of loadData(2), and if it's 0, return service.loadData(3) ?

非常欢迎任何帮助或提示:)

Any help or tips are very welcome :)

推荐答案

一种简单直接的方法是分别订阅每个呼叫并检查其结果.

One straight forward and ugly way would be to subscribe to each call individually and check it's result.

getData(): Observable<number> {
  const $subject = new Subject<number>();

  service.loadData(1).subscribe(
    loadOneResponse => {
      if(loadOneResponse.length === 0) {
        service.loadData(2).subscribe(
          loadTwoResponse => {
            if(loadOneResponse.length === 0) {
              service.loadData(3).subscribe(
                loadThreeResponse => {
                  $subject.next(loadThreeResponse);
                  $subject.complete();
                },
                loadThreeError => {
                }
              );
            } else {
              $subject.next(loadTwoResponse);
              $subject.complete();
            }
          },
          loadTwoError => {
          }
        );
      } else {
        $subject.next(loadOneResponse);
        $subject.complete();
      }
    },
    loadOneError => {
    }
  );

  return $subject.asObservable();
}

这篇关于Angular-多个条件HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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