Limit Promise Concurrency with pool

Methods like Promise.all, Promise.allSettled, Promise.race, and the rest are really excellent for managing multiple Promises, allowing for our apps to embrace async and performance. There are times, however, that limiting the number of concurrent operations may be useful, like rate limiting or simply not wanting to put a server under massive stress. Enter an simple […]

The post Limit Promise Concurrency with pool appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

Methods like Promise.all, Promise.allSettled, Promise.race, and the rest are really excellent for managing multiple Promises, allowing for our apps to embrace async and performance. There are times, however, that limiting the number of concurrent operations may be useful, like rate limiting or simply not wanting to put a server under massive stress.

Enter an simple utility for limiting Promise concurrency: pool!

import pool from '@ricokahler/pool';

async function getQuotes() {
  const quotes = await pool({
    collection: [1, 2, 3, 4, 5],
    maxConcurrency: 2, // Limit 2 requests at a time
    task: async (symbol) => {
      const response = await fetch(`/quotes/${symbol}`);
      const json = await response.json();
      return json;
    },
  });

  console.log(quotes); // Array of the 5 quotes
}

pool lets you specify how many requests to run concurrently. If no concurrency value is provided, pool acts like Promise.all.

Concurrency is an important issue with JavaScript’s async nature, so having a method for pooling them together and limiting concurrent actions is important.

The post Limit Promise Concurrency with pool appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2020-11-03T15:35:54+00:00) Limit Promise Concurrency with pool. Retrieved from https://www.scien.cx/2020/11/03/limit-promise-concurrency-with-pool/

MLA
" » Limit Promise Concurrency with pool." David Walsh | Sciencx - Tuesday November 3, 2020, https://www.scien.cx/2020/11/03/limit-promise-concurrency-with-pool/
HARVARD
David Walsh | Sciencx Tuesday November 3, 2020 » Limit Promise Concurrency with pool., viewed ,<https://www.scien.cx/2020/11/03/limit-promise-concurrency-with-pool/>
VANCOUVER
David Walsh | Sciencx - » Limit Promise Concurrency with pool. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/11/03/limit-promise-concurrency-with-pool/
CHICAGO
" » Limit Promise Concurrency with pool." David Walsh | Sciencx - Accessed . https://www.scien.cx/2020/11/03/limit-promise-concurrency-with-pool/
IEEE
" » Limit Promise Concurrency with pool." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2020/11/03/limit-promise-concurrency-with-pool/. [Accessed: ]
rf:citation
» Limit Promise Concurrency with pool | David Walsh | Sciencx | https://www.scien.cx/2020/11/03/limit-promise-concurrency-with-pool/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.