Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 2-使用flatmap的同步http调用在返回空的可观察对象时不执行下一个调用_Angular_Http_Rxjs_Flatmap - Fatal编程技术网

Angular 2-使用flatmap的同步http调用在返回空的可观察对象时不执行下一个调用

Angular 2-使用flatmap的同步http调用在返回空的可观察对象时不执行下一个调用,angular,http,rxjs,flatmap,Angular,Http,Rxjs,Flatmap,我正在以这种方式执行三个同步调用 this.deletePreallocations() .flatMap(()=>{ 返回此.postreallocations(); }) .flatMap(()=>{ 退回这个。延期付款(); }) .takeWhile(()=>this.isAlive) .订阅( () => { }, 错误=>{ 控制台日志(err); });这是正确的,因为flatMap仅适用于next通知和Observable.empty()仅发送完成的通知,而不发送其他通知 因此

我正在以这种方式执行三个同步调用

this.deletePreallocations()
.flatMap(()=>{
返回此.postreallocations();
})
.flatMap(()=>{
退回这个。延期付款();
})
.takeWhile(()=>this.isAlive)
.订阅(
() => { },
错误=>{
控制台日志(err);

});
这是正确的,因为
flatMap
仅适用于
next
通知和
Observable.empty()
仅发送
完成的
通知,而不发送其他通知

因此,您可以做的是不依赖于下一个通知,而只是等待上一个可观察到的完成:

this.deletePreallocations()
     .concat(Observable.defer(() => this.postPreallocations()))
     .concat(Observable.defer(() => this.postPayment()))
     .takeWhile(() => this.isAlive)
     .subscribe(
         () => { },
         err => {
            console.log(err);
         }
     );
我使用的是
Observable.defer
,它只在您订阅时调用它的回调。由于在
this.postreallocations()
this.postPayment()
中,您有一些依赖于内部状态的逻辑,因此应该保证只有当
concat
尝试订阅时才会调用这些方法