srmagura/real-cancellable-promise

How does this compare to https://github.com/alkemics/CancelablePromise

Closed this issue · 4 comments

I noticed your prior art didn't list this package: https://github.com/alkemics/CancelablePromise

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.

real-cancellable-promise

  • When creating a 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.
  • If a CancellablePromise is canceled, it rejects with a Cancellation object which you can catch using try-catch.
  • If it's not possible to cancel the underlying asynchronous operation, you can use the pseudoCancellable function which causes the CancellablePromise to reject on cancellation without affecting the underlying asynchronous operation.

cancelable-promise

  • It looks like the default usage is something like this: new CancelablePromise((resolve) => setTimeout(resolve, 1)). The underlying asynchronous operation is not canceled. This is analogous to pseudoCancellable in my library.
  • It look like, if a CancelablePromise is canceled, it neither resolves nor rejects.
  • You can use the 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.