Hacker News Viewer

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&#x27;re not forced to support cancellation, but it&#x27;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 &quot;wrong&quot; way to do this as well. The Java thread library provides a stop() function. But since that&#x27;s exogenous, it doesn&#x27;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. &quot;Cooperative&quot; (as opposed to preemptive) cancel is much cleaner.

4/7/2026, 2:31:43 PM


by: eithed

&gt; 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:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;Promise" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;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 &quot;how to cancel a promise&quot; and the first results were Christian blogs about how you can&#x27;t cancel a promise to god etc. Things haven&#x27;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&#x27;ll create a memory leak. It&#x27;s easy to end up with a leak that&#x27;s almost impossible to track down, because there isn&#x27;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

&gt; Libraries like Effect have increased the popularity of generators, but it&#x27;s still an unusual syntax for the vast majority of JavaScript developers.<p>I&#x27;m getting so tired of hearing this. I loved the article and it&#x27;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&#x27;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 &#x27;Pinky&#x27; 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&#x27;t target it) also it&#x27;s faster and used by the top startups of YC so it&#x27;s very sexy and it can now be your girlfriend so you don&#x27;t need a fake AI girlfriend and devs don&#x27;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