.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / p-limit / index.js
1 'use strict';
2 const pTry = require('p-try');
3
4 module.exports = concurrency => {
5         if (concurrency < 1) {
6                 throw new TypeError('Expected `concurrency` to be a number from 1 and up');
7         }
8
9         const queue = [];
10         let activeCount = 0;
11
12         const next = () => {
13                 activeCount--;
14
15                 if (queue.length > 0) {
16                         queue.shift()();
17                 }
18         };
19
20         return fn => new Promise((resolve, reject) => {
21                 const run = () => {
22                         activeCount++;
23
24                         pTry(fn).then(
25                                 val => {
26                                         resolve(val);
27                                         next();
28                                 },
29                                 err => {
30                                         reject(err);
31                                         next();
32                                 }
33                         );
34                 };
35
36                 if (activeCount < concurrency) {
37                         run();
38                 } else {
39                         queue.push(run);
40                 }
41         });
42 };