Can and should I use JS AbortController like .NET CancellationToken?

56 views Asked by At

I recently found out about AbortController and its sibling AbortSignal to register event listeners until I don't want it anymore.

When I look at the documentation, it looks surprisingly similar to .NET CancellationTokenSource and CancellationToken, the controller has abort() method, the signal has aborted property and even a throwIfAborted() method.

However I am not sure if it was designed to be used internally by APIs like fetch and addEventListener only or can I use it for my own APIs like this?

function doSomething(input, ct) {
   ct?.throwIfAborted();
   await this.doStuff();
   ct?.throwIfAborted();
   // ...
}

Is there anything I should notice coming from .NET background that I should be aware of?

0

There are 0 answers