Using Observable when expecting only one single event.

Writing code with observables forces extra nesting which becomes difficult to read.

My suggestion, when using an observable with a single event or when only the first or the last is the one we need to handle - use the await / async pattern

Since RxJS version 7+ we can do it like this:

async () => {

    let result = await firstValueFrom(this.someObservable());

}

In previous versions of RxJS we could use toPromise(), but now it is marked for deprecation in future versions.

Post a Comment

Previous Post Next Post