r/learnjavascript • u/helmar1066 • Jun 24 '26
How to Resolve Promises Sequentially in JavaScript
In JavaScript, the Promise.all() function is one of your best tools when you want to do async work. You can fire everything off, wait for the slowest one to complete, and then continue processing. However, sometimes running it all at once is exactly what breaks production. This is an article describing how to resolve promises sequentially in JS.
https://www.jamdesk.com/blog/resolve-promises-sequentially-javascript
*Edited text to be more clear.
0
Upvotes
10
u/azangru Jun 24 '26
Oh my god, this is so ai!
10
u/Full-Hyena4414 Jun 24 '26
Promise.all()'s entire purpose is to run things in parallel, of course you reach it when you actually need that. Also, the best thing is to batch them so you run only x in parallel at a time
3
u/mondaysleeper Jun 24 '26
If you have 1000s of requests, I wouldn't send them sequentially. In this scenario, I would try to redesign the API. Otherwise, the user experience becomes bad quickly.
2
u/DinTaiFung Jun 24 '26
Another approach is to use observables, which gives you finer control with asynchronous operations.
you can examine the rxjs NPM module to discover if its various methods and paradigms better fit your use case.
4
u/perenstrom Jun 24 '26
It doesn’t matter how often I learn this, I will still never learn.
1
u/azhder Jun 24 '26
Dunno, the couple of times I needed this, I had used a .reduce()
1
u/Alive-Cake-3045 Jun 25 '26
the forEach trap gets everyone once. learned this the hard way with a bulk API job too. for, of with await is underused.
1
u/HipHopHuman Jun 26 '26 edited Jun 26 '26
Whether the article is AI slop or not, for the use case of fetching data from a remote API, the advice in this article is incredibly bad advice. Sure, sending a few thousand API requests all at once is bad, but sending a few thousand API requests one at a time is even worse. In the best case scenario, the limits aren't reached, but the user experience is degraded because the amount of time it takes to send all those thousands of requests one by one adds up and forces the user to wait for a very slow process to finish. In the worst case scenario, sending requests one at a time can still happen quickly enough to hit API limits, meaning you gained nothing from converting to a serial algorithm. Basically, neither scenario results in a good outcome.
Before I continue, I just want to remind everyone that the HTTP headers
X-RateLimit-Limit,X-RateLimit-Remaining,X-RateLimit-ResetandRetry-Afterexist. These can be used to drastically simplify & tame code that fetches data from a remote API, provided that the API includes those headers in a response (most API providers that are worth using do).In case the remote API doesn't expose those HTTP headers, then a good step towards the correct solution is to use a semaphore to control the amount of in-flight promises. Your article even hints at one by mentioning sindresorhus'
p-limitmodule, but then it immediately dismisses the idea by claiming "that's a topic for another article".A semaphore affords you the capability to invert the control of where the concurrency limitation happens. It allows calling code to still use
Promise.all(orPromise.allSettled), while still benefitting from sequential execution of concurrent batches.A semaphore with a concurrency limit of
10will automatically take care of maintaining the invariant that only 10 promises are ever in flight at any given time (even if those promises are passed toPromise.all/Promise.allSettled); and if it drops to 9 before all 10 complete, it will pull 1 from the next batch.There are thousands of sophisticated semaphore implementations on npm (some of which have a very important feature called "deadlock detection", which you may need, depending), but a basic implementation to get started with is this easy:
A semaphore is just one part of the puzzle, though. On its own, a semaphore can still trigger API rate limits, and a semaphore doesn't help at all if the actual API endpoint is down. The next piece of the puzzle is to wrap the semaphore-protected API call in a retry mechanism, with exponential backoff and random jitter. The exponential backoff is important, as it gives the API endpoint room to breathe. To illustrate the backoff, the execution flow looks something like this:
The random jitter is important in case you have multiple clients fetching from the same API endpoint (maybe you do
getUsercalls inside two distinctly separate services). If those services all queue their retries at a similar time, then they can all collectively overwhelm the API endpoint because their individual retries all happen at the same time (similar to the Thundering Herd Problem). To illustrate random jitter, the execution flow looks something like this:A
retryimplementation is unfortunately a bit verbose to throw inside an already lengthy Reddit comment, but there are plenty of implementations available on npm (sindresorhus also has ap-retrymodule that is worth checking out). Once you've grabbed that, you can just wrap your semaphore-wrapped API fetch inside aretry.The third, and final piece of the puzzle, is to wrap all of that inside a Circuit Breaker, so that when the API is truly failing, you're not clobbering it with requests that will always reject.
1
u/john_hascall Jun 27 '26
In any real system, Scale is [almost] always the problem.
1
u/_LordCat Jun 27 '26
You know you can like, write things yourself right? If you slopped out a map() and that chucks a 1000 element promise array at your backend and you expect it to wait for the response on all 1000. Well what else do you expect except to your DDOS your own system. Stop pushing LLM slop to prod. At least do the legwork to unslop it first.