Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Handling several promises in Javascript

Let's say I want to run a lot of promises (which are not yet declared, so their body has not executed yet). I don't want to run them in sequence because that would be very slow but I don't want all of them to run in parallel because that would overload the server. Each of this promises can be resolve or rejected very fast (around 1 second) or, in the worst case scenario, after something around 40 seconds. None of the promises depend on the result of any other. Which is the best way to handle this case? I have seen that an alternative is using Promise.all() in batches of 10 promises. However, the latter doesn't seem to be efficient considering that I can have 9 promises resolving very fast, but 1 of them resolving very slow. What I would like the following:

Say I have 100 promises. I start processing 10 of them, as soon as one of them resolves, the 11th promise is processed, as soon as other one resolves, the 12th promise is processed, and so on. Notice this behavior is different from using Promise.all() in batches of 10 because in that case the 11th to 20th promise would be processed after all of the 1st to 10th promises are resolved.

Which is the best way of implementing this? I was picturing something like having a queue and having an async function processPromise that pops promises from that queue (until it is empty) and process them. I would then make an array of promises where each promise corresponds to an invocation of processPromise and then Promise.all() over that array. However, I'm not sure if concurrent access to that queue (that is share by different invocations of processPromise) would be an issue, or if I have nothing to worry about considering javascript is single-threaded.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have used promise-pool for this:

        const { results } = await PromisePool.withConcurrency(10)
            .for(tasks)
            .process(async task => {
                const result = await this.longTask(task);
                return { result };
            });

Note that surprisingly results are not always returned in the order you provided them, so depending on your use case you may have to track the original index and sort upon completion:

        const tasks = items.map((item, index) => ({ item, index }));
        const { results } = await PromisePool.withConcurrency(10)
            .for(tasks)
            .process(async task => {
                const result = await this.longTask(task.item);
                return { result, index: task.index };
            });
        const ordered = results
            .sort((a, b) => a.index - b.index)
            .map(result => result.result);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!