c5f1905db947a6b1e464f2a50a0b5c3aa6dc3ea5994110d9b2a61c9c2e5d8ef11805a7fceaa94e7318c37ca9ac345992f90fec7fa8b3ef0b67e8d6ef6aeb02 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # p-limit [![Build Status](https://travis-ci.org/sindresorhus/p-limit.svg?branch=master)](https://travis-ci.org/sindresorhus/p-limit)
  2. > Run multiple promise-returning & async functions with limited concurrency
  3. ## Install
  4. ```
  5. $ npm install p-limit
  6. ```
  7. ## Usage
  8. ```js
  9. const pLimit = require('p-limit');
  10. const limit = pLimit(1);
  11. const input = [
  12. limit(() => fetchSomething('foo')),
  13. limit(() => fetchSomething('bar')),
  14. limit(() => doSomething())
  15. ];
  16. (async () => {
  17. // Only one promise is run at once
  18. const result = await Promise.all(input);
  19. console.log(result);
  20. })();
  21. ```
  22. ## API
  23. ### pLimit(concurrency)
  24. Returns a `limit` function.
  25. #### concurrency
  26. Type: `number`<br>
  27. Minimum: `1`
  28. Concurrency limit.
  29. ### limit(fn)
  30. Returns the promise returned by calling `fn`.
  31. #### fn
  32. Type: `Function`
  33. Promise-returning/async function.
  34. ## Related
  35. - [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
  36. - [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
  37. - [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
  38. - [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
  39. - [More…](https://github.com/sindresorhus/promise-fun)
  40. ## License
  41. MIT © [Sindre Sorhus](https://sindresorhus.com)