-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add raceAndCancel
helper function for structured concurrency
#11
Add raceAndCancel
helper function for structured concurrency
#11
Conversation
Motivation: The core concept of Structured concurrency [1] requires that "all spawned threads have completed before exit". The classic implementation of `Promise.race` doesn't meet this requirement, as the returned promise finishes sooner than any of the other promises and the other promises are kept running until completion. Therefore, the `Promise.race` violates the basic principle of structured concurrency when used as a building block for combining parallel tasks. We propose a simple wrapper for the `Promise.race` that ensures all remaining promises are automatically cancelled upon completion of the first one. We name the wrapper `raceAndCancel`, however, we are open to any other naming suggestions. Example of use: ``` const result = await raceAndCancel([ someLongOperation(), CancellablePromise.delay(timeout) ]) ``` [1] https://en.wikipedia.org/wiki/Structured_concurrency
This PR is a draft, if the concept of |
This is an example how a timeout for a (cancellable) promise can be implemented using
|
Hey @ondrej-stanek-ozobot, nice work. My only concern is, I am not sure how frequently this new function would be used, and I would prefer to keep the API surface area of this library relatively small. Is there a reason why Some ideas for how we could proceed:
Thanks 😀 |
to correctly work with `Awaited<>`
I totally understand, no problem. We are using this (and other) helper functions quite extensively in our codebase, and the We are thinking about publishing the helpers for structured concurrency. If the intention is to keep Because the |
Publishing your structured concurrency helper functions as a new npm package is a cool idea! Let me know if you end up doing that. |
Motivation:
The core concept of Structured concurrency [1] requires that "all spawned threads have completed before exit".
The classic implementation of
Promise.race
doesn't meet this requirement, as the returned promise finishes sooner than any of the other promises and the other promises are kept running until completion. Therefore, thePromise.race
violates the basic principle of structured concurrency when used as a building block for combining parallel tasks.We propose a simple wrapper for the
Promise.race
that ensures all remaining promises are automatically cancelled upon completion of the first one. We name the wrapperraceAndCancel
, however, we are open to any other naming suggestions.Example of use:
[1] https://en.wikipedia.org/wiki/Structured_concurrency