▲ | kaoD 2 days ago | |
Why this isn't possible is implicitly (well, somewhat explicitly) addressed in my comment.
No matter what you do it's impossible to cancel this promise unless `someLibary` exposes some way to cancel `expensiveComputation`, and you somehow expose a way to cancel it (and any other await points) and any other promises it uses internally also expose cancellation and they're all plumbed to have the cancellation propagated inward across all their await points.Unsubscribing to the completion listener is never enough. Implementing cancellation in your outer promise is never enough. > The javascript promise provides the minimum language implementation upon which more elaborate Promise implementations can be constructed. I'll reiterate: there is no way to write promise cancellation in JS userspace. It's just not possible (for all the reasons outlined in my long-ass comment above). No matter how elaborate your implementation is, you need collaboration from every single promise that might get called in the call stack. The proposed `unpromise` implementation would not help either. JS would need all promises to expose a sort of `AbortController` that is explicitly connected across all cancellable await points inwards which would introduce cancel-safety issues. So you'd need something like this to make promises actually cancelable:
And you need all promises to get their `signal` properly propagated (and properly handled) across the whole call stack. |