RxJS v7 is deprecating the .toPromise() method of observable objects, and fixing the type definition to return Promise<T | undefined> as there is no way for the type system to know if the observable will always produce at least one notification so the resolve value may be undefined in all places where .toPromise() is used (though it's made impossible by using certain operators).
This method is replaced with two helpers:
firstValueFrom(x$: Observable<T>): Promise<T>: equivalent of x$.pipe(first()).toPromise(), resolves with the first value, unsubscribes after the first notification, rejects if o$ completes without any notifications.
lastValueFrom(x$: Observable<T>): Promise<T>: equivalent of x$.pipe(last()).toPromise(), resolves after the observable completes with the last notification value, rejects if o$ completes without any notifications.
We should implement these methods in @kbn/std and start migrating parts of the code base to these helpers. Once RxJS v7 is released we can replace our helpers with the standard ones.
If we can get most or all of the codebase migrated to use the new helpers before RxJS v7 is released then we can use an eslint rule to prevent new usage of toPromise(), or just fixup the remaining issues when the final upgrade happens.
RxJS v7 is deprecating the
.toPromise()method of observable objects, and fixing the type definition to returnPromise<T | undefined>as there is no way for the type system to know if the observable will always produce at least one notification so the resolve value may be undefined in all places where.toPromise()is used (though it's made impossible by using certain operators).This method is replaced with two helpers:
firstValueFrom(x$: Observable<T>): Promise<T>: equivalent ofx$.pipe(first()).toPromise(), resolves with the first value, unsubscribes after the first notification, rejects ifo$completes without any notifications.lastValueFrom(x$: Observable<T>): Promise<T>: equivalent ofx$.pipe(last()).toPromise(), resolves after the observable completes with the last notification value, rejects ifo$completes without any notifications.We should implement these methods in
@kbn/stdand start migrating parts of the code base to these helpers. Once RxJS v7 is released we can replace our helpers with the standard ones.If we can get most or all of the codebase migrated to use the new helpers before RxJS v7 is released then we can use an eslint rule to prevent new usage of
toPromise(), or just fixup the remaining issues when the final upgrade happens.