Sometimes, we want to to call finally on subscribing to observable with Rxjs and JavaScript.
In this article, we’ll look at how to to call finally on subscribing to observable with Rxjs and JavaScript.
How to call finally on subscribing to observable with Rxjs and JavaScript?
To to call finally on subscribing to observable with Rxjs and JavaScript, we call pipe
on the observable we subscribed to.
For instance, we write
const 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(value),
(error) => console.log(error),
() => console.log("complete")
);
to create an Observable
.
And then we call pipe
to emit the data.
Then we call pipe
with the observable returned by finalize
.
We call finalize
with a callback that runs when all the observable code is run.
pipe
returns another observable that we call subscribe
on to get the data from pipe
.
Conclusion
To to call finally on subscribing to observable with Rxjs and JavaScript, we call pipe
on the observable we subscribed to.