Ami*_*ugi 84 javascript rxjs rxjs5
根据这个artcle,onComplete
和onError
功能subscribe
是互斥的.
任何意义onError
或onComplete
事件将在我的事件中激起subscribe
.
我有一个逻辑块,无论是否收到错误都需要执行,或者我成功完成了大量的信息.
我finally
在python中查找类似的内容,但我发现的只是finally
需要附加到我创建的可观察对象上.
但是我想在我订阅时,在流结束后,无论是成功还是出错,都要做这个逻辑.
有任何想法吗?
mar*_*tin 104
调用此运算符的当前"pipable"变体finalize()
.调用了较旧且现已弃用的"补丁"运算符finally()
.
我认为finalize()
运营商实际上是正确的.你说:
只有在我订阅时以及流结束后才能执行该逻辑
我认为这不是问题.如果需要,您可以在订阅之前source
使用一个并使用finalize()
它.这样您就不需要总是使用finalize()
:
let source = new Observable(observer => {
observer.next(1);
observer.error('error message');
observer.next(3);
observer.complete();
}).pipe(
publish(),
);
source.pipe(
finalize(() => console.log('Finally callback')),
).subscribe(
value => console.log('#1 Next:', value),
error => console.log('#1 Error:', error),
() => console.log('#1 Complete')
);
source.subscribe(
value => console.log('#2 Next:', value),
error => console.log('#2 Error:', error),
() => console.log('#2 Complete')
);
source.connect();
Run Code Online (Sandbox Code Playgroud)
这打印到控制台:
#1 Next: 1
#2 Next: 1
#1 Error: error message
Finally callback
#2 Error: error message
Run Code Online (Sandbox Code Playgroud)
2019年1月:更新了RxJS 6
Har*_*Das 39
唯一对我有用的是这个
fetchData()
.subscribe(
(data) => {
//Called when success
},
(error) => {
//Called when error
}
).add(() => {
//Called when operation is complete (both success and error)
});
Run Code Online (Sandbox Code Playgroud)
pca*_*sme 19
我现在在Angular应用程序中使用RxJS 5.5.7并且使用finalize
运算符对我的用例有一个奇怪的行为,因为在成功或错误回调之前触发.
简单的例子:
// Simulate an AJAX callback...
of(null)
.pipe(
delay(2000),
finalize(() => {
// Do some work after complete...
console.log('Finalize method executed before "Data available" (or error thrown)');
})
)
.subscribe(
response => {
console.log('Data available.');
},
err => {
console.error(err);
}
);
Run Code Online (Sandbox Code Playgroud)
我不得不add
在订阅中使用medhod来完成我想要的东西.基本上是finally
在成功或错误回调完成后的回调.像try..catch..finally
块或Promise.finally
方法.
简单的例子:
// Simulate an AJAX callback...
of(null)
.pipe(
delay(2000)
)
.subscribe(
response => {
console.log('Data available.');
},
err => {
console.error(err);
}
);
.add(() => {
// Do some work after complete...
console.log('At this point the success or error callbacks has been completed.');
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
59075 次 |
最近记录: |