Skip to content
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

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

Closed
CMCDragonkai opened this issue Apr 1, 2022 · 4 comments

Comments

@CMCDragonkai
Copy link

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

@srmagura
Copy link
Owner

srmagura commented Apr 1, 2022

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.

@CMCDragonkai
Copy link
Author

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?

@srmagura
Copy link
Owner

srmagura commented Apr 3, 2022

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.

@CMCDragonkai
Copy link
Author

Great turns out that even FS has AbortController.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants