readme.md
  1  # p-limit [![Build Status](https://travis-ci.org/sindresorhus/p-limit.svg?branch=master)](https://travis-ci.org/sindresorhus/p-limit)
  2  
  3  > Run multiple promise-returning & async functions with limited concurrency
  4  
  5  ## Install
  6  
  7  ```
  8  $ npm install p-limit
  9  ```
 10  
 11  ## Usage
 12  
 13  ```js
 14  const pLimit = require('p-limit');
 15  
 16  const limit = pLimit(1);
 17  
 18  const input = [
 19  	limit(() => fetchSomething('foo')),
 20  	limit(() => fetchSomething('bar')),
 21  	limit(() => doSomething())
 22  ];
 23  
 24  (async () => {
 25  	// Only one promise is run at once
 26  	const result = await Promise.all(input);
 27  	console.log(result);
 28  })();
 29  ```
 30  
 31  ## API
 32  
 33  ### pLimit(concurrency)
 34  
 35  Returns a `limit` function.
 36  
 37  #### concurrency
 38  
 39  Type: `number`\
 40  Minimum: `1`\
 41  Default: `Infinity`
 42  
 43  Concurrency limit.
 44  
 45  ### limit(fn, ...args)
 46  
 47  Returns the promise returned by calling `fn(...args)`.
 48  
 49  #### fn
 50  
 51  Type: `Function`
 52  
 53  Promise-returning/async function.
 54  
 55  #### args
 56  
 57  Any arguments to pass through to `fn`.
 58  
 59  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.
 60  
 61  ### limit.activeCount
 62  
 63  The number of promises that are currently running.
 64  
 65  ### limit.pendingCount
 66  
 67  The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).
 68  
 69  ### limit.clearQueue()
 70  
 71  Discard pending promises that are waiting to run.
 72  
 73  This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.
 74  
 75  Note: This does not cancel promises that are already running.
 76  
 77  ## FAQ
 78  
 79  ### How is this different from the [`p-queue`](https://github.com/sindresorhus/p-queue) package?
 80  
 81  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 the queue.
 82  
 83  ## Related
 84  
 85  - [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
 86  - [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
 87  - [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
 88  - [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
 89  - [More…](https://github.com/sindresorhus/promise-fun)
 90  
 91  ---
 92  
 93  <div align="center">
 94  	<b>
 95  		<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>
 96  	</b>
 97  	<br>
 98  	<sub>
 99  		Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
100  	</sub>
101  </div>