Closed CMCDragonkai closed 2 years ago
Good question! real-cancellable-promise
and cancelable-promise
can do the same things, but the two libraries have different underlying philosophies and behaviors.
The big idea behind real-cancellable-promise
is that promise.cancel()
should truly cancel the API call, animation, or whatever other operation the promise represents.
CancellablePromise
instance, you provide a normal Promise
and a cancel
function. The cancel
function is supposed to cancel the underlying asynchronous operation. This is how real-cancellable-promise
is intended to be used.CancellablePromise
is canceled, it rejects with a Cancellation
object which you can catch using try-catch
.pseudoCancellable
function which causes the CancellablePromise
to reject on cancellation without affecting the underlying asynchronous operation.new CancelablePromise((resolve) => setTimeout(resolve, 1))
. The underlying asynchronous operation is not canceled. This is analogous to pseudoCancellable
in my library.CancelablePromise
is canceled, it neither resolves nor rejects. onCancel
callback to cancel the underlying asynchronous operation.Oh that's great. I am actually looking to really cancel the underlying async operation.
For alot of web requests this can be done with abort controller.
What are your thoughts regarding other kinds of async operations like file writing... Etc?
real-cancellable-promise
should work well with AbortController
.
I am not really a Node.js guy, so I don't know much about file I/O in JavaScript. But real-cancellable-promise
should work fine here as long as you can create a cancel
function that aborts the file writing operation.
Great turns out that even FS has AbortController
.
I noticed your prior art didn't list this package: https://github.com/alkemics/CancelablePromise