alt.hn

4/7/2026 at 1:34:33 PM

You can't cancel a JavaScript promise (except sometimes you can)

https://www.inngest.com/blog/hanging-promises-for-control-flow

by goodoldneon

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

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.

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.

by pjc50

4/7/2026 at 3:16:52 PM

I am surprised that you had to go out of your way to remove Thread.stop from existing Java code. It's been deprecated since 1998, and the javadoc page explains pretty clearly why it's inherently unsafe.

It's hard to miss all the warnings unless you're literally just looking at the method name and nothing else.

by teraflop

4/7/2026 at 3:19:56 PM

I was certainly surprised to see it when I found it.

by pjc50

4/7/2026 at 6:24:10 PM

Not to mention that I feel like it's pretty unusual to be creating and managing threads yourself in Java these days, instead of using a thread pool/executor.

by kelnos

4/7/2026 at 4:53:56 PM

That’s barely-junior interview question indeed.

by wiseowise

4/7/2026 at 6:19:26 PM

One of Java's the ecosystem fundamental platforms is that it's multi-threading. It's gone through too many models.

And since Java has a metric ton of blog posts from the 2000s and 2010s, a lot of search engines lead you to older models.java itself has gone from green threads to OS threads and back to green threads now.

by AtlasBarfed

4/8/2026 at 9:26:36 AM

Write one, stop anywhere.

by DonHopkins

4/7/2026 at 2:52:14 PM

AbortSignal is same thing on the Web. It's unfortunate TC39 failed to ever bring a CancelToken to the language to standardize the pattern outside browsers.

by esprehn

4/7/2026 at 7:01:08 PM

Browsers have said that they are unwilling to ship any new cancelation mechanisms given that AbortSignal already exists, so we can't ship a different CancelToken. But I think there's a path to standardizing a subset of the existing AbortSignal machinery [1].

(I am on TC39 and while this isn't my highest priority I did bring the topic for discussion at the last meeting [2], and there was support from the rest of committee.)

[1] https://github.com/tc39/proposal-concurrency-control/issues/...

[2] https://github.com/bakkot/structured-concurrency-for-js

by bakkoting

4/8/2026 at 2:33:22 AM

Yes, I was around during the original discussions. AbortSignal exists because TC39 was taking too long and cancelling fetch() is table stakes for a networking oriented platform like the web.

Those threads are a good example of what's wrong in TC39. A simple AbortSignal could have been added, but by the time it's reconciled with SES, large speculative APIs like Governers, or the original attempt to add a parallel throw mechanism just for cancellation, nothing actually gets done.

It's been 10 years since CancelToken was first discussed and we're still debating it.

by esprehn

4/8/2026 at 3:02:30 PM

I agree that the original cancellation discussion was bad. I don't agree that these threads reflect the same disfunction. They're a new effort (from me). No one was working on it previously because browsers have said that they were unwilling to add any other form of cancellation given that AbortSignal already exists, so there was never a chance to add a separate CancelToken once it shipped. The work to be done now is basically administrative: moving a subset from the WHATWG spec to TC39. This has ~no relevance to user's lives unless they're using a JS runtime which does not implement the WinterTC spec, which is approximately no one. The delay has nothing to do with SES (which has no bearing on this), and Governors are a _use case_ which motivates bringing it into the language, not a thing with which it needs to be reconciled.

by bakkoting

4/8/2026 at 5:37:56 AM

Unless I'm missing something, AbortSignal is quite standardized on backend as well.

Of course not all libraries support it, but many do, and support seems to be growing.

by watermelon0

4/7/2026 at 3:04:28 PM

TC39 seems to be failing at many things for the past 10 years.

by runarberg

4/7/2026 at 6:26:15 PM

Hard disagree, TC39 has done great work over the last 10 years. To name a few: - Async/await - Rest/spread - Async iterators - WeakRefs - Explicit Resource Management - Temporal

It's decisions are much more well thought out than WHATWG standards. AbortSignal extending from EventTarget was a terrible call.

by notnullorvoid

4/7/2026 at 8:28:34 PM

many things !== all the things

More good works from the last 10 years includes .at(), nullish chaining, BigInt etc.

But most of what you mentioned is closing in on 10 years in the standard (Async/Await is from 2017) meaning the bulk of the work done is from over 10 years ago.

The failure of AbortSignal is exactly the kind of failure TC39 has been doing in bulk lately. I have been following the proposal to add Observables to the language, which is a stage 1 proposal (and has been for over 10 years!!!). There were talks 5 years ago (!) to align the API with AbortSignal[1] which I think really exemplifies the inability for TC39 to reach a workable decision (at least as it operates now).

Another example I like to bring up are the failure of the pipeline operator[2], which was advanced to stage-2 four years ago and has been in hiatus ever since with very little work to show for it. After years of deliberation very controversal version of the operator with a massive community backlash. Before they advanced it it was one of the more popular proposals, now, not so much, and personally I sense any enthusiasm for this feature has pretty much vanished. In other words I think they took half a decade to make the obviously wrong decision, and have since given up.

From the failure of the pipeline operator followed a bunch of half-measures such as array grouping, and iterator helpers etc. which could have easily been implemented in userland libraries if the more functional version of the pipeline operator would have advanced.

1: https://github.com/tc39/proposal-observable/issues/209

2: https://github.com/tc39/proposal-pipeline-operator

by runarberg

4/7/2026 at 5:23:32 PM

I don't like it - you're forced to pass around this token, constantly manage the lifecycle of cancellation sources - and incredibly bug prone thing in async context, and it quickly gets very confusing when you have multiple tokens/sources.

I understand why they did it - a promise essentially is just some code, and a callback that will be triggered by someone at some point in time - you obviously get no quality of service promises on what happens if you cancel a promise, unless you as a dev take care to offer some.

It's also obvious that some operations are not necessarily designed to be cancellable - imagine a 'delete user' request - you cancelled it, now do you still have a user? Maybe, maybe you have some cruft lying around.

But still, other than the obvious wrong solution - C# had a Thread.Abort() similar to the stop() function that you mentioned, that was basically excommunicated from .NET more then a decade ago, I'm still not happy with the right one.

by torginus

4/7/2026 at 6:12:17 PM

    > ...constantly manage the lifecycle of cancellation sources
Very rare unless you are spawning your own.

Usually, you are passing through a runtime provided token (e.g. ASP.NET).

by CharlieDigital

4/7/2026 at 6:25:00 PM

Not that rare in my experience, I constantly had to write software like this. Not every day, but it certainly did come up quite often in my code and others'

Oh and oone more thing - the very (developer-managed) complexity makes it that people constantly got it wrong, usually just enough (as often with the case of threading) that it worked fine 90% of the time, and was very hard to make a case to management why we should invest effort into fixing it.

by torginus

4/7/2026 at 10:12:11 PM

Not rare in the slightest. C# is used in a lot of places that aren't the web and don't have extra frameworks piled on.

If you're writing a bare C# library for desktop deployment, you're managing your own cancellation sources.

by estimator7292

4/7/2026 at 10:10:15 PM

Cancelling a token doesn't immediately abort the underlying Task. It is up to the implementation of that task to poll the token and actively decide when to abort.

In your example, you'd design your delete task such that if you want it to be cancelable, it can only be canceled before data is modified. You simply don't abort in the middle of a database transaction.

Moreover, because of the way cancellation tokens work, you can't abort blocking function calls unless you also pass the token along. There just isn't a mechanism that can interrupt a long IO operation or whatever unless you explicitly go to the effort to make that happen.

A cancellation token is more of a "pretty please stop what you're doing when you feel like it" concept than Thread.Abort().

by estimator7292

4/7/2026 at 2:43:43 PM

C# has very good support for this.

You can even link cancellation tokens together and have different cancellation "roots".

by CharlieDigital

4/7/2026 at 4:53:12 PM

> "Cooperative" (as opposed to preemptive) cancel is much cleaner.

Which what Thread.interrupt does.

by wiseowise

4/7/2026 at 3:03:38 PM

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!)

by mohsen1

4/8/2026 at 1:33:04 PM

Microsoft supports something god doesn't? That can't be right.

by phendrenad2

4/7/2026 at 4:14:05 PM

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.

by thomasnowhere

4/7/2026 at 2:37:53 PM

GC can be very slow. Relying on it for control flow is a bold move

by cush

4/7/2026 at 3:49:59 PM

I don't think the control flow relies on GC.

The control flow stops because statements after `await new Promise(() => {});` will never run.

GC is only relied upon to not create a memory leak, but you could argue it's the same for all other objects.

by BlueGreenMagick

4/7/2026 at 4:15:29 PM

as long as there's no leak interrupting a promise should be good for performance overall, not necessarily for the front-end but for the whole chain.

by dominicrose

4/7/2026 at 4:03:04 PM

Not that very slow for web applications. Maybe for real time or time-sensitive applications. For most day to day web apps GC pauses are mostly unnoticeable, unless you are doing something very wrong

by augusto-moura

4/7/2026 at 3:25:17 PM

and so the thirty year old hackathon continues...

by abraxas

4/7/2026 at 6:45:50 PM

Is it safe to just "stop calling next() on a generator?" like the post suggest?

To me that sounds like dropping the task on the floor. Specifically, this will not invoke any finally {} blocks:

More correctly, you should invoke `return()` on the generator. Otherwise, you won't provide execution guarantees. This is how Effection does it. There is no equivalent in async functions, so it sounds like the same problem would apply to the GC technique.

by cowboyd

4/7/2026 at 4:30:05 PM

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.

by bastawhiz

4/7/2026 at 4:33:59 PM

This is addressed at the end of the article:

  The catch

  You're relying on garbage collection, which is nondeterministic. You don't get to know when the suspended function is collected. For our use case, that's fine. We only need to know that it will be collected, and modern engines are reliable about that.

  The real footgun is reference chains. If anything holds a reference to the hanging promise or the suspended function's closure, the garbage collector can't touch it. The pattern only works when you intentionally sever all references.

by hungryhobbit

4/7/2026 at 6:14:08 PM

That should honestly be much higher up and much more clearly spelled out.

by bastawhiz

4/7/2026 at 7:00:32 PM

The argument against rejecting to cancel seems like a stretch to me. It's completely fine if you view cancellation as a error condition, it allows you to recover from a cancellation if you want (swallow the error w catch) or to propagate it.

by notnullorvoid

4/7/2026 at 5:39:42 PM

I soft of feel like every five years someone comes along and tries to re-invent cancellable threads and immediately arrives back at the same conclusion: the problem of what it means to "cancel" a thread is so domain-specific that you never save anything trying to "support" it in your threading framework; you try and save people the effort of doing something ad-hoc to simulate cancellation and build something at least as complicated as what they would build ad-hoc, because thread cancellation is too intimately tied to the problem domain the threads are operating on to generalize it.

by shadowgovt

4/8/2026 at 12:13:20 AM

I would also argue that Rust failed to cancel a Future too, considering I came from a C++ and C# background where I know a lot more about async/await and the missing of "cancellation token" in Tokio is so infuriating.

I have to came all the way to attach it to a Future, that because Rust doesn't have any default argument, I mean I have to supply cancellation token from the top to bottom.

But in hindsight, Golang's context actually do mimick cancellation token in some sort, but in a way that the cancellation is cascaded and recursive by using done in canceler or deadline which means it is timing and latency sensitive.

If you truly want cancellation token style, you need to use semaphores or by explicitly polling the cancellation channel (which is what you can do with context) which can hurt if you don't have enough thread slack.

by stevefan1999

4/7/2026 at 2:21:18 PM

Off topic, but that site has really nice design

by game_the0ry

4/7/2026 at 2:46:24 PM

Mh, I couldn't read due to the huge contrast and had to switch to reader mode, so...

by williamdclt

4/7/2026 at 2:49:35 PM

I personally find it to be perfectly readable. I've heard of people with issues with white text on a black background, but I don't fully understand it. Do you have astigmatism?

by jazzypants

4/7/2026 at 3:59:40 PM

What colors were you seeing? It's light white text on a black background for me-- both super common and plenty readable.

by seattle_spring

4/7/2026 at 6:10:12 PM

Really? I generally very much like to have a lot of contrast, but too much can definitely hurt my eyes.

by Pay08

4/7/2026 at 3:08:47 PM

I mean, I'm not a designer but it was interesting enough to call out.

by game_the0ry

4/7/2026 at 2:14:48 PM

> Libraries like Effect have increased the popularity of generators, but it's still an unusual syntax for the vast majority of JavaScript developers.

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??

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.

by dimitropoulos

4/7/2026 at 2:46:55 PM

There just aren't that many spots where the average js dev actually needs to touch a generator.

I don't really see generators ever crossing into mainstream usage in the same way as the other features you've compared them to. Most times... you just don't need them. The other language tools solve the problem in a more widely accessible manner.

In the (very limited & niche) subset of spots you do actually need a generator, they're nice to have, but it's mostly a "library author" tool, and even in that scope it's usage just isn't warranted all that often.

by horsawlarway

4/7/2026 at 4:27:26 PM

mainly because they messed up on implementation, in two ways. This is of course my opinion.

The first being `.next()` on the returned iterators. If you pass an argument to it, the behavior is funky. The first time it runs, it actually doesn't capture the argument, and then you can capture the argument by assigning `yield` to a variable, and do whatever, but its really clunky from an ergonomic perspective. Which means using it to control side effects is clunky.

The second one how it is not a first class alternative to Promise. Async Generators are not the most ergonomic thing in the world to deal with, as you have the issues above plus you have to await everything. Which I understand why, but because generators can't be used in stead of Promises, you get these clunky use cases for using them.

They're really only useful as a result, for creating custom iterator patterns or for a form of 'infinite stream' returns. Beyond that, they're just not all that great, and it often takes combining a couple generators to really get anything useful out of them.

Thats been my experience, and I've tried to adopt generators extensively a few times in some libraries, where I felt the pattern would have been a good fit but it simply didn't turn out most of the time.

by no_wizard

4/7/2026 at 7:41:14 PM

I think writing an effect library yourself is a tough ask, but some of them have gotten really, really good. And they get you things that are simply not possible with promise. Check out Effection if you want a more vanilla javascript syntax, or Effect if you're really into expressing things functionally.

by cowboyd

4/7/2026 at 3:49:13 PM

It is a specialised instrument but a useful one: batch processing and query pagination are first class use cases for generators that can really simplify business logic code. Stream processing is another and in fact Node.js streams have had a generator API for several releases now.

by gbuk2013

4/7/2026 at 2:25:23 PM

Generators peaked in redux- saga and thunk days before we had widespread support for async/await.

You're right, mostly pointless syntax (along with Promise) now that we can await an async function anyway, especially now with for .. of to work with Array methods like .map

But there are still some use cases for it, like with Promise. Like for example, making custom iterators/procedures or a custom delay function (sync) where you want to block execution.

by yeittrue

4/7/2026 at 6:07:02 PM

Ten years ago, I was an acid reader of the tc39 (EcmaScript standard committee) mailing list and cancelable promises used to be the hot topic for a while.

I unsubscribed at some point because I wasn't working with JavaScript this much, but it's disappointing to see that this work has gone nowhere in the meantime.

I like how Rust futures are canceled by dropping them, even though it's also a footgun (though IMHO this is more of a problem with the select! pattern than with drop-to-cancel proper).

by littlestymaar

4/7/2026 at 3:28:05 PM

You can also race it with another promise, which e.g. resolves on timeout.

by afarah1

4/7/2026 at 4:34:12 PM

You can but it still won't get cancelled. I found out when I tried to implement a hard time limit to a call.

by Keyframe

4/7/2026 at 6:22:25 PM

[dead]

by ryguz

4/7/2026 at 3:31:35 PM

[flagged]

by TZubiri

4/7/2026 at 4:00:52 PM

> If I know the javascript ecosystem, and I think I do

You think you do, but...

by seattle_spring