angular 4 个顺序异步 http 请求,错误响应时取消
angular 4 sequential async http requests with cancellation on error response
我正在尝试制作 3 个顺序和异步(none 并行),使用 HttpClient 和 Observable.forkJoin.
到目前为止是这样的,首先是 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 方法,但什么是?
如何进行 none 并行订阅,只有前一个订阅成功才能继续下一个订阅?
使用 Observables 执行此操作的正确方法是什么?
我想 concatAll
就是您要找的。查看 rxjs documentation。
按照@MichaelKie 的建议使用Observable.concat,我设法通过以下方式解决了这个问题。
let requestPayloadObjects:any[] = [{...},{...},{...}];
let firstServerCall:Observable<any>;
this.requestPayloadObjects.forEach( ( payloadObject:any,i:number )=> {
let observable = this._uploadApi.postDoc(payloadObject:any);
if(i===0) {
firstServerCall = observable ;
}
else {
firstServerCall = firstServerCall.concat(observable );
};
});
firstServerCall.subscribe((res:any)=> {
// 1. will get here after every successful response
// 2. will continue on to send next http call after this
},(err:any)=> {
// will get here if subscription fails, without continuing to the next observable.
})
我正在尝试制作 3 个顺序和异步(none 并行),使用 HttpClient 和 Observable.forkJoin.
到目前为止是这样的,首先是 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 方法,但什么是? 如何进行 none 并行订阅,只有前一个订阅成功才能继续下一个订阅? 使用 Observables 执行此操作的正确方法是什么?
我想 concatAll
就是您要找的。查看 rxjs documentation。
按照@MichaelKie 的建议使用Observable.concat,我设法通过以下方式解决了这个问题。
let requestPayloadObjects:any[] = [{...},{...},{...}];
let firstServerCall:Observable<any>;
this.requestPayloadObjects.forEach( ( payloadObject:any,i:number )=> {
let observable = this._uploadApi.postDoc(payloadObject:any);
if(i===0) {
firstServerCall = observable ;
}
else {
firstServerCall = firstServerCall.concat(observable );
};
});
firstServerCall.subscribe((res:any)=> {
// 1. will get here after every successful response
// 2. will continue on to send next http call after this
},(err:any)=> {
// will get here if subscription fails, without continuing to the next observable.
})