Angular 如何检查两个http调用是否都失败?

Angular 如何检查两个http调用是否都失败?,angular,promise,rxjs,angular-promise,Angular,Promise,Rxjs,Angular Promise,我想进行2次http post调用,如果两次调用都失败,则显示一个错误,如果其中一次调用返回数据,则我不想显示错误 this.http.post<any[]>(URL, jsonBody1, postJson) //returns an Observable this.http.post<any[]>(URL, jsonBody2, postJson) //returns an Observable this.http.post(URL,jsonBody1,postJs

我想进行2次http post调用,如果两次调用都失败,则显示一个错误,如果其中一次调用返回数据,则我不想显示错误

this.http.post<any[]>(URL, jsonBody1, postJson) //returns an Observable
this.http.post<any[]>(URL, jsonBody2, postJson) //returns an Observable
this.http.post(URL,jsonBody1,postJson)//返回一个可观察的
this.http.post(URL,jsonBody2,postJson)//返回一个可观察的
我可以通过将http post转换为承诺来实现这一点吗?我尝试了下面的代码,但没有成功。如果第一个then()抛出错误,它跳过第二个then(),进入catch(),但是如果第一个then()抛出错误,我希望它执行下一个then()

this.http.post<any[]>(URL, jsonBody1, postJson).toPromise()
  .then( data => {
    // do something with data
  })
  .then( _ =>
    this.http.post<any[]>(URL, jsonBody2, postJson).toPromise()
      .subscribe( data => {
          // do something with data
        })
  )
  .catch( error => {
    console.log(error);
  });
this.http.post(URL,jsonBody1,postJson).toPromise()
。然后(数据=>{
//处理数据
})
.然后(=>
this.http.post(URL,jsonBody2,postJson).toPromise()
.订阅(数据=>{
//处理数据
})
)
.catch(错误=>{
console.log(错误);
});
您可以使用Promise.all()

Promise.all(
this.http.post(URL,jsonBody1,postJson).toPromise(),
this.http.post(URL,jsonBody2,postJson).toPromise()
)。然后(响应=>{
让[response1,response2]=响应
}).catch(错误=>{
console.log({err})
})

您可以仅使用
可观察的
进行操作,而不将其更改为
承诺

forkJoin(
this.http.post<any[]>(URL, jsonBody1, postJson),
this.http.post<any[]>(URL, jsonBody2, postJson)).subscribe (
    x => console.log(x),
    error => console.log(error)
    () => console.log('completed'))
forkJoin(
this.http.post(URL,jsonBody1,postJson),
this.http.post(URL,jsonBody2,postJson)).subscribe(
x=>console.log(x),
error=>console.log(错误)
()=>console.log('completed'))

上述方法可能比在RxJS 6中使用
Promise

更简单。您可以通过组合
forkJoin
catchError
map
操作符来实现这一点:

import { forkJoin, of, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';

const a$ = of(1);
// const a$ = throwError(new Error('Error in $a'));
// const b$ = of('a');
const b$ = throwError(new Error('Error in $b'));

forkJoin(
    a$.pipe(catchError(() => of(null))),
    b$.pipe(catchError(() => of(null))),
  ).pipe(
    map(results => {
      if (results.some(Boolean)) {
        return results;
      }
      throw new Error(`It's broken.`)
    })
  )
  .subscribe(console.log, console.warn);
见现场演示:

只有当两个源都是
false
(catch error替换为
false
)时,您才会收到错误。

简单方法

this.http.post<any[]>(URL, jsonBody1, postJson).toPromise()
  .then( data => {
    // do something with data
  })
  .then( _ =>
    this.http.post<any[]>(URL, jsonBody2, postJson).toPromise()
      .subscribe( data => {
          // do something with data
        })
  )
  .catch( error => {
    console.log(error);
  });
this.http.post<any[]>(URL, jsonBody1, postJson).subscribe(
  data => {
     this.http.post<any[]>(URL2, jsonBody2, postJson).subscribe(
        data2 => {
           // both calls succeeded
        }
        error => {
           // set error variable to true
        }
     )
  }
  error => {
    // set error variable to true
  }
)
this.http.post(URL,jsonBody1,postJson).subscribe(
数据=>{
this.http.post(URL2,jsonBody2,postJson).subscribe(
数据2=>{
//两个电话都成功了
}
错误=>{
//将错误变量设置为true
}
)
}
错误=>{
//将错误变量设置为true
}
)

这两个调用相互依赖。我的意思是你想打第一个电话,什么时候打第二个电话?@martin,这两个电话是独立的,但是如果其中一个返回结果,那么它就不会显示错误。但如果两者都返回HttpErrorResponse,那么它将显示error。很抱歉,它对我不起作用。您从何处导入forkJoin?导入forkJoin时,您的导入位于顶部,如下所示:从“rxjs”导入{forkJoin};