You can't cancel a JavaScript promise (except sometimes you can)
by goodoldneon on 4/7/2026, 1:34:33 PM
https://www.inngest.com/blog/hanging-promises-for-control-flow
Comments
by: pjc50
I like how C# handles this. You're not forced to support cancellation, but it's strongly encouraged. The APIs all take a CancellationToken, which is driven by a CancellationTokenSource from the ultimate caller. This can then either be manually checked, or when you call a library API it will notice and throw an OperationCancelledException.<p>Edit: note that there is a "wrong" way to do this as well. The Java thread library provides a stop() function. But since that's exogenous, it doesn't necessarily get cleaned up properly. We had to have an effort to purge it from our codebase after discovering that stopping a thread while GRPC was in progress broke all future GRPC calls from all threads, presumably due to some shared data structure being left inconsistent. "Cooperative" (as opposed to preemptive) cancel is much cleaner.
4/7/2026, 2:31:43 PM
by: eithed
> Promise itself has no first-class protocol for cancellation, but you may be able to directly cancel the underlying asynchronous operation, typically using AbortController.<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...</a>
4/7/2026, 2:42:49 PM
by: mohsen1
Back in 2012 I was working on a Windows 8 app. Promises were really only useful on the Windows ecosystem since browser support was close to non existent. I googled "how to cancel a promise" and the first results were Christian blogs about how you can't cancel a promise to god etc. Things haven't changes so much since, still impossible to cancel a promise (I know AbortSignal exists!)
4/7/2026, 3:03:38 PM
by: bastawhiz
Be careful with this, though. If a promise is expected to resolve and it never does, and the promise needs to resolve or reject to clean up a global reference (like an event listener or interval), you'll create a memory leak. It's easy to end up with a leak that's almost impossible to track down, because there isn't something obvious you can grep for.
4/7/2026, 4:30:05 PM
by: thomasnowhere
The never-resolving promise trick is clever but what caught me off guard is how clean the GC behavior is. Always assumed hanging promises would leak in long-lived apps but apparently not as long as you drop the references.
4/7/2026, 4:14:05 PM
by: cush
GC can be very slow. Relying on it for control flow is a bold move
4/7/2026, 2:37:53 PM
by:
4/7/2026, 2:26:45 PM
by: abraxas
and so the thirty year old hackathon continues...
4/7/2026, 3:25:17 PM
by: dimitropoulos
> Libraries like Effect have increased the popularity of generators, but it's still an unusual syntax for the vast majority of JavaScript developers.<p>I'm getting so tired of hearing this. I loved the article and it's interesting stuff, but how many more decades until people accept generators as a primitive??<p>used to hear the same thing about trailing commas, destructuring, classes (instead of iife), and so many more. yet. generators still haven't crossed over the magic barrier for some reason.
4/7/2026, 2:14:48 PM
by: game_the0ry
Off topic, but that site has really nice design
4/7/2026, 2:21:18 PM
by: TZubiri
If I know the javascript ecosystem, and I think I do, this is an opportunity for some undergrad from Kazakhstan to create a library called 'Pinky' that offers unbreakable promises, which will have 1M downloads on npm and 10K stars on github, and will allow the dev to get a US Visa and employment.<p>The library will get additional maintainers until it balloons into 100Kloc with features like reading config files, which would need to eventually get split into a transitive dependency called configy, until one day a maintainer clicks on an enlarge penis link or gets phished by a fake AI girlfriend that was actually a russian dude, and it hits half of the javascript ecosystem because it had become a transitive dependency for every single package, and then everyone switches to a new package manager that somehow survived this due to a security feature, (but it was actually because no one uses that package manager and so the attacker didn't target it) also it's faster and used by the top startups of YC so it's very sexy and it can now be your girlfriend so you don't need a fake AI girlfriend and devs don't get supply chained anymore.
4/7/2026, 3:31:35 PM
by: afarah1
You can also race it with another promise, which e.g. resolves on timeout.
4/7/2026, 3:28:05 PM