首页 > 解决方案 > 角度 4 顺序异步 http 请求,错误响应取消

问题描述

我正在尝试使用HttpClientObservable.forkJoin进行 3 个顺序和异步(非并行)请求。

到目前为止,这是它的外观,首先是fileOperations数组:

requestPayloadObjects:any[];//array of objects sent to the same request 

//... after the array was built
this.requestPayloadObjects.forEach( ( payloadObject:any,i:number )=> {
   //_uploadApi.postDoc returns an http observable
   fileOperations.push( this._uploadApi.postDoc( payloadObject ) );
});

然后在我有一个可观察的数组之后,我使用Observable.forkJoin

// parallel subscriptions, all 3 observables are subscribed to asyncly
Observable.forkJoin(fileOperations).subscribe((res:any)=> {
        // If all http calls succeeded you get here
    },(err:any)=> {
        // If 1 of all 3 that were sent failed you get here
    });    

但是我想要的是:

// NONE-parallel subscription  
Observable.forkJoin(fileOperations).subscribe((res:any)=> {
        // If all http calls succeeded you get here
    },(err:any)=> {
        // If 1 failed, you get here, but you do not send the others
    });    

我确定 Observable.forkJoin 不是实现此目的的 Observable 方法,但它是什么?如何进行非并行订阅,只有在前一个成功的情况下才会继续下一个?使用 Observables 执行此操作的正确方法是什么?

标签: angularangular5angular6angular2-observablesangular-httpclient

解决方案


我想concatAll这就是你要找的。查看它的rxjs 文档


推荐阅读