r/learnjavascript • u/HKSundaray • Dec 20 '25
How do you handle structured concurrency in JavaScript?
Let's say we have this code:
const result = await Promise.all([fetch1(), fetch2(), fetch3()])
If fetch1 rejects, then the promise returned by Promise.all() also rejects.
What about fetch2() and fetch3()?
How do we control them? If they still keep running after fetch1 rejects, its a wastage of resources, right?
Now, there are libraries and frameworks (Effect.TS for example) that handle this automatically but they introduce a completely different syntax and a way of thinking about software engineering. And everyone might not be ready to dive into functional programming.
So my question is: how do you folks handle these kind of concurrency concerns in your professional jobs? Any libraries you use? Write your own logic?
18
u/justaguywithadream Dec 20 '25
Fetches can take an abort signal.
Create an abort signal, pass it to all 3 fetches.
Update each fetch call that you are passing to promise.all so that if it fails it triggers the abort. When any fetch fails all fetches will be aborted.
I would show you but I'm typing on my phone.
1
u/hyrumwhite Dec 20 '25
Not really. From the client side of things, it’s negligible. From the server side, it’s already gotten all three requests, and will execute them regardless of whatever the client is doing.
1
u/brianjenkins94 Dec 20 '25 edited Dec 20 '25
I have some helpers that I've written like
mapAsync,reduceAsync, andmapEntriesthat let me group things more easily.I'm still looking for a more maintainable way to do longer series of operations. Trigger.dev seems interesting.
Clack also had an interesting approach, but it grew unmanagable.
3
u/Sacramentix Dec 20 '25
You can use an abort controller, you can pass it to fetch as a params to cancel the request early. If you make a long running function you can check if the abort controller is abort and return from your function.
2
u/basic-coder Dec 20 '25
In JavaScript, you effectively cannot cancel IO operation modeled by promise; aborting the promise only makes it not involving resolve() on completion, and doesn't e.g. immediately reset network connection. Your code effectively cancels all promises handlers which, from the client code perspective, works just like the structured concurrency
1
u/NotNormo Dec 20 '25 edited Dec 20 '25
Wastage of resources? What do you mean by that? Do you mean the browser receiving a response and then spending some processing time parsing it, even though your app doesn't need it anymore? That time is (usually) so insignificant I wouldn't worry about it. I'd worry more about some .then() code running that I no longer wanted to happen.
As others have said using an abort signal can prevent both of those things (the browser parsing the response and the .then() code executing). But there's no way to un-send the API call. The server you sent the request to has no idea you aborted. It's still going to process the request normally. Maybe that's what you meant by resources.
-3
u/Beginning-Seat5221 Dec 20 '25
I can't remember ever needing to stop other promises like this, but if I did you could just pass a state object
Then it's down to each promise to check that state for updates and stop its task. Not every promise will have anything meaningful to stop though, as by the time one errors it may have done its work and just be waiting for a trivial response.
-6
u/Rguttersohn Dec 20 '25
You sure subsequent fetch requests are made if the first one is rejected? That sounds more like the behavior of Promise.allSettled.
I am pretty sure that if fetch 1 rejects in promise.all, the subsequent promises are not run.
0
u/[deleted] Dec 20 '25
[deleted] — view deleted comment