Is there a way to detect when a Subscription has closed? I have a loading component that displays a loading message when a Subscription exists && is not closed, and otherwise displays the content, but once closed I wanted to reset the variable that was referencing the subscription, otherwise I can't use mySubscription.closed
as an useful indicating in the template.
Yes, based on the RxJS subscribe() documentation there are 3 arguments that can be passed, last one being the OnCompleted callback.
var observer = Rx.Observer.create(
function (x) {
console.log('Next: %s', x);
},
function (err) {
console.log('Error: %s', err);
},
function () {
console.log('Completed');
});
A couple other options.
tap()
and finalize()
These are both operators that you put in a pipe(...)
- so the logic runs independent of the subscribe(...)
call which you may not even be in your control.
finalize()
is called oncompletion
ORerror
tap()
has three parametersnext
,error
, andcomplete
that are called on those conditions
Finalize should probably be reserved for true cleanup type tasks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With