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?
View all comments
Show parent comment
1
u/NotNormo Dec 20 '25 edited Dec 20 '25
Conceivably, an API could be designed such that a GET endpoint might be expected to take a long time due to multiple slow steps being done in the back-end. Another API endpoint could be created that would allow the client to cancel it's previous GET request that hasn't finished yet. And maybe that cancellation could save the back-end app from doing some unnecessary operations. (a HTTP response would still get sent back to the client but maybe it would be some sort of error/aborted response)
I can't think of an API like this. This would be very complex and would probably result in a ton of bug-prone race conditions but it's still conceivable. (EDIT: I did a bit more googling and found that such a slow GET would not be good API design. Instead, the GET should respond quickly with info about the back-end job that has started. And there would be another endpoint the client could call to cancel the job.)
But typical APIs don't provide a way for the client to cancel the processing of a request. If a request has been received, the back-end will process it and then send a response back to the client. The back-end app would not know whether the client still wants it or not.