Is there a way to attach a handler when an observable starts executing (someone called subscribe on it)?
like in angular:
this.http.post('someUrl', resource).pipe(
catchError(),
finalize((() => this.hideLoader()),
**executing(() => this.showLoader()) <------**
)
An Observable Allows its Observers to Unsubscribe The subscribe() method returns a subscription object which may be used to cancel the subscription. Calling unsubscribe() clears the resources used by the subscription and calls the teardown function returned by the subscriber function.
In comparison to a regular Observable, a Subject allows values to be multicasted to many Observers. A Subject and its subscribers have a one-to-many relationship. A Subject can be an Observable as well as an Observer. They hold a registry of many listeners to multiple Observables.
defer allows you to create an Observable only when the Observer subscribes. It waits until an Observer subscribes to it, calls the given factory function to get an Observable -- where a factory function typically generates a new Observable -- and subscribes the Observer to this Observable.
Promises deal with one asynchronous event at a time, while observables handle a sequence of asynchronous events over a period of time. Emit multiple values over a period of time.
The defer
observable factory function is most likely what you are looking for:
import { defer } from 'rxjs';
const post = defer(() => {
this.showLoader();
return this.http.post('someUrl', resource).pipe(
catchError(),
finalize(() => this.hideLoader())
);
});
post.subscribe();
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