I recenlty was reviewing a project where Angular observables are heavily utilized across both components and services, as to “reactive programming approach”. I’ve noticed the use of of(undefined)
in many observable chains.
While I understand that of(undefined)
serves as an initial trigger in observable streams, I’m curious about how widespread this approach is among developers and whether it’s considered a best practice for specific scenarios.
Here’s an example to illustrate how this is used:
private doAction(): Observable<void> {
return of(undefined).pipe(
switchMap(() => {
// perform actions
return of(undefined);
}),
catchError((err) => {
console.error('Error occurred:', err);
return of(undefined); // Handle error by returning another undefined observable
}),
finalize(() => {
// cleanup code here
})
);
}