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
6## Install
7
8```
9$ npm install p-limit
10```
11
12
13## Usage
14
15```js
16const pLimit = require('p-limit');
17
18const limit = pLimit(1);
19
20const input = [
21	limit(() => fetchSomething('foo')),
22	limit(() => fetchSomething('bar')),
23	limit(() => doSomething())
24];
25
26(async () => {
27	// Only one promise is run at once
28	const result = await Promise.all(input);
29	console.log(result);
30})();
31```
32
33
34## API
35
36### pLimit(concurrency)
37
38Returns a `limit` function.
39
40#### concurrency
41
42Type: `number`<br>
43Minimum: `1`
44
45Concurrency limit.
46
47### limit(fn, ...args)
48
49Returns the promise returned by calling `fn(...args)`.
50
51#### fn
52
53Type: `Function`
54
55Promise-returning/async function.
56
57#### args
58
59Any arguments to pass through to `fn`.
60
61Support 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.
62
63### limit.activeCount
64
65The number of promises that are currently running.
66
67### limit.pendingCount
68
69The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).
70
71
72## FAQ
73
74### How is this different from the [`p-queue`](https://github.com/sindresorhus/p-queue) package?
75
76This 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.
77
78
79## Related
80
81- [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
82- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
83- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
84- [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
85- [More…](https://github.com/sindresorhus/promise-fun)
86
87
88## License
89
90MIT © [Sindre Sorhus](https://sindresorhus.com)
91