You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.5 KiB

4 years ago
  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`<br>
  28. Default: `Infinity`
  29. Concurrency limit.
  30. ### limit(fn, ...args)
  31. Returns the promise returned by calling `fn(...args)`.
  32. #### fn
  33. Type: `Function`
  34. Promise-returning/async function.
  35. #### args
  36. Any arguments to pass through to `fn`.
  37. Support for passing arguments on to the `fn` is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a *lot* of functions.
  38. ### limit.activeCount
  39. The number of promises that are currently running.
  40. ### limit.pendingCount
  41. The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).
  42. ## FAQ
  43. ### How is this different from the [`p-queue`](https://github.com/sindresorhus/p-queue) package?
  44. This package is only about limiting the number of concurrent executions, while `p-queue` is a fully featured queue implementation with lots of different options, introspection, and ability to pause and clear the queue.
  45. ## Related
  46. - [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
  47. - [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
  48. - [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
  49. - [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
  50. - [More…](https://github.com/sindresorhus/promise-fun)
  51. ---
  52. <div align="center">
  53. <b>
  54. <a href="https://tidelift.com/subscription/pkg/npm-p-limit?utm_source=npm-p-limit&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  55. </b>
  56. <br>
  57. <sub>
  58. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  59. </sub>
  60. </div>