Using AbortSignal and AbortController
See original GitHub issueThe standards proposals are leaning towards using AbortSignal and AbortController (ala fetch
cancellation) for cancellation with Observables. I generally find this favorable for a few reasons and I think I see a migration path for us.
Proposal
- Observable
subscribe
acceptsObserver
and an optionalAbortSignal
. - Observable
forEach
accepts an optionalAbortSignal
. - The subscriber function for
new Observable(subscriber)
gives both a safeObserver
and anAbortSignal
, but is expected to return void. - The
Observer
passed to subscribe will have an optionalabort
handler that will get theAbortError
if the observable is aborted. - If an abort is triggered on a subscription that happened with
forEach
, theAbortError
would go down the rejection path just like it does with fetch. Since the returned promise is a guarantee to a future where the producer either errors or completes, it makes sense to send theAbortError
down the rejection path in this case.
Advantages
- Synchronous observables cannot firehose and lock up an app, the cancellation semantic exists prioer to subscription.
- If multiple data producers are set up in a subscriber function (in the Observable constructor), you can add them to the abort signal one at a time, guaranteeing they can be torn down if a subsequent step fails. This wasn’t possible before without some serious hackery.
- Observables all the way down. Since
AbortSignal
is an EventTarget, and EventTarget may soon have anon
method that returns an observable. you could useabortSignal.on('abort')
to compose teardown if you so choose. - We will now have the ability to be notified, uniquely, of cancellation (without having to do trickery to figure out if it was really an error or a completion)
Migration path
We’ll start using AbortSignal
internally throughout the library.
- Add an overload to
subscribe
that accepts anObserver
and anAbortSignal
. IfAbortSignal
is passed, it does not return aSubscription
. - Make
Subscription
into anAbortController
- Add the ability to create an
AbortSignal
. - Alias
unsubscribe
asabort
.
- Have the subscriber function passed to the
Observable
constructor receive anAbortSignal
. - Any function/Subscription returned from the subscriber function (from the constructor), will automatically be added to the
AbortSignal
. - Remove the observer
start
method I just added. 😃 lol It’s no longer necessary. - Add the observer
abort
method that acceptsAbortError
. - Ensure that aborts arriving at the tail-end of a
forEach
get sent as rejections of typeAbortError
to the returnedPromise
. - BONUS: Have
toPromise
accept an abort signal - Add deprecation messages for usages that don’t align with the spec.
- Long term, I’d like to phase out the 3 handlers method:
subscribe(fn, fn , fn)
, because object literals are more readable anyhow, anforEach
can be used for the general case of just needingnext
. Thinksubscribe(null, (err) => doAThing(err))
vssubscribe({ error: doAThing })
NOTE: The current TC39 proposal may not reflect this yet
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:39 (24 by maintainers)
Top Results From Across the Web
AbortController & AbortSignal | Can I use... Support ... - CanIUse
Controller object that allows you to abort one or more DOM requests made with the Fetch API. Usage % of. all users, all...
Read more >AbortController - Web APIs | MDN
Returns an AbortSignal object instance, which can be used to communicate with, or to abort, a DOM request. Instance methods. AbortController.
Read more >The complete guide to AbortController in Node.js
This tutorial will be a complete guide to the AbortController and AbortSignal APIs. Contents. Introduction to the AbortController API; How to ...
Read more >How to Cancel Promise with AbortController - LeanyLabs
Now, it's preferred to use AbortController that provides an abort() method that triggers the aborted state of the AbortSignal object obtained from signal ......
Read more >Create an abortable API using AbortController and AbortSignal
Create an abortable API using AbortController and AbortSignal. Recently I worked on a project that required me to build a typeahead search UI...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I wrapped my proposal into a library of functions that can be used as drop-in replacement for RxJS factories/operators where AbortSignal makes sense: https://github.com/felixfbecker/abortable-rx
It’s a bit more nuanced:
AbortSignal
that’s already aborted, as that will not fire theabort
event anymoreabort
listener if the Observable completes, errors, or gets unsubscribed from, else it’s gonna leak memory (could do this by usingtakeUntil(fromEvent(signal, 'abort').pipe(take(1)))
instead of a Subject)