-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
firstValueFrom/lastValueFrom is missing AbortSignal support #6442
Comments
Hi @ronag I see your point, but maybe the idea is to use use something like
if you want to cancel. this will than reject your promise. |
No... the correct way would be something like: await rxjs.firstValueFrom(x$.pipe(rxjs.fromEvent(signal, 'abort'), rxjs.throwError(() => new AbortError())) Which is kind of a mouthfull... in general promise APi's should support the |
Why do you think that is the correct way? Why do you think promise API's should support the AbortSignal? Dont get me wrong. I do not know what is intended. Im just trying to understand the issue and help if I can. The way I see it is that the AbortSignal is often used as a mechanism to cancel (reject) promises. But the |
Taking into account that Also, the implementation of this custom operator seems to be pretty trivial... I haven't tested it, but I think that this should do it: const abortableFirstValueFrom = <T>(
source$: Observable<T>,
signal: AbortSignal
): Promise<T> => {
if (signal.aborted) return Promise.reject(new AbortError())
const signalError$ = fromEvent(signal, "abort").pipe(
map(() => {
throw new AbortError();
})
);
return firstValueFrom(merge(source$, signalError$));
}; |
That's a good question. This is related to discussions (e.g. #5545 ) in RxJS about building on and supporting modern cross-platform primitives. It is the "official guidance" of Node.js and WHATWG that the way to handle cancellation in promise returning APIs is with AbortSignal. We have also added AbortSignal support to all of our own APIs in recent versions. We don't own the way people write code. RxJS is welcome to do something different from Node.js here - and we are not remotely forcing (or even strongly suggesting) adoption. I think @ronag's ask is reasonable (support AbortSignal) in APIs that return promises (like .forEach and firstValueFrom). |
I agree. I would also like to add it to forEach, but that needs to be more carefully considered, I think. |
I'll have a go at this soon. |
Adds a feature to the `firstValueFrom` config to support passing an `AbortSignal` that can be used to unsubscribe from the underlying subscription. This will result in the returned promise rejecting with an `AbortError`, which is an error type belonging to the library at this point. This is because there is no consistent error type to throw across all supported runtimes that the user could check for. related ReactiveX#6442
Similar to the update to `firstValueFrom`. This adds a configuration option to unsubscribe from the underlying subscription with an `AbortSignal`. If aborted with a signal, the returned promise will reject with an `AbortError`. resolves ReactiveX#6442
@benlesh I dont see how |
Promise based API's should generally also accept a
signal
property which is passed anAbortSignal
instance.The text was updated successfully, but these errors were encountered: