Observable 最后订阅
Observable Finally on Subscribe
根据this artcle,subscribe
的onComplete
和onError
函数是互斥的。
意味着 onError
或 onComplete
事件将在我的 subscribe
中触发。
我有一个逻辑块,无论我收到错误还是成功完成我的信息流都需要执行。
我查找了类似 finally
in python, but all I found is finally
的东西,它需要附加到我创建的可观察对象上。
但我只想在订阅时以及流结束后执行该逻辑,无论是成功还是出错。
有什么想法吗?
此运算符的当前 "pipable" 变体称为 finalize()
(自 RxJS 6 起)。较旧且现已弃用的 "patch" 运算符称为 finally()
(直到 RxJS 5.5)。
我认为 finalize()
运算符实际上是正确的。你说:
do that logic only when I subscribe, and after the stream has ended
我认为这不是问题。如果需要,您可以有一个 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();
这会打印到控制台:
#1 Next: 1
#2 Next: 1
#1 Error: error message
Finally callback
#2 Error: error message
2019 年 1 月:针对 RxJS 6 更新
我现在在 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);
}
);
我不得不在订阅中使用 add
方法来完成我想要的。基本上是在成功或错误回调完成后的 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.');
});
唯一对我有用的是这个
fetchData()
.subscribe(
(data) => {
//Called when success
},
(error) => {
//Called when error
}
).add(() => {
//Called when operation is complete (both success and error)
});
根据this artcle,subscribe
的onComplete
和onError
函数是互斥的。
意味着 onError
或 onComplete
事件将在我的 subscribe
中触发。
我有一个逻辑块,无论我收到错误还是成功完成我的信息流都需要执行。
我查找了类似 finally
in python, but all I found is finally
的东西,它需要附加到我创建的可观察对象上。
但我只想在订阅时以及流结束后执行该逻辑,无论是成功还是出错。
有什么想法吗?
此运算符的当前 "pipable" 变体称为 finalize()
(自 RxJS 6 起)。较旧且现已弃用的 "patch" 运算符称为 finally()
(直到 RxJS 5.5)。
我认为 finalize()
运算符实际上是正确的。你说:
do that logic only when I subscribe, and after the stream has ended
我认为这不是问题。如果需要,您可以有一个 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();
这会打印到控制台:
#1 Next: 1
#2 Next: 1
#1 Error: error message
Finally callback
#2 Error: error message
2019 年 1 月:针对 RxJS 6 更新
我现在在 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);
}
);
我不得不在订阅中使用 add
方法来完成我想要的。基本上是在成功或错误回调完成后的 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.');
});
唯一对我有用的是这个
fetchData()
.subscribe(
(data) => {
//Called when success
},
(error) => {
//Called when error
}
).add(() => {
//Called when operation is complete (both success and error)
});