Changeset e29cc2e for trip-planner-front/node_modules/p-limit/index.js
- Timestamp:
- 11/25/21 22:08:24 (3 years ago)
- Branches:
- master
- Children:
- 8d391a1
- Parents:
- 59329aa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/node_modules/p-limit/index.js
r59329aa re29cc2e 1 1 'use strict'; 2 const Queue = require('yocto-queue');2 const pTry = require('p-try'); 3 3 4 4 const pLimit = concurrency => { 5 5 if (!((Number.isInteger(concurrency) || concurrency === Infinity) && concurrency > 0)) { 6 throw new TypeError('Expected `concurrency` to be a number from 1 and up');6 return Promise.reject(new TypeError('Expected `concurrency` to be a number from 1 and up')); 7 7 } 8 8 9 const queue = new Queue();9 const queue = []; 10 10 let activeCount = 0; 11 11 … … 13 13 activeCount--; 14 14 15 if (queue. size> 0) {16 queue. dequeue()();15 if (queue.length > 0) { 16 queue.shift()(); 17 17 } 18 18 }; 19 19 20 const run = async(fn, resolve, ...args) => {20 const run = (fn, resolve, ...args) => { 21 21 activeCount++; 22 22 23 const result = (async () => fn(...args))();23 const result = pTry(fn, ...args); 24 24 25 25 resolve(result); 26 26 27 try { 28 await result; 29 } catch {} 30 31 next(); 27 result.then(next, next); 32 28 }; 33 29 34 30 const enqueue = (fn, resolve, ...args) => { 35 queue.enqueue(run.bind(null, fn, resolve, ...args)); 36 37 (async () => { 38 // This function needs to wait until the next microtask before comparing 39 // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously 40 // when the run function is dequeued and called. The comparison in the if-statement 41 // needs to happen asynchronously as well to get an up-to-date value for `activeCount`. 42 await Promise.resolve(); 43 44 if (activeCount < concurrency && queue.size > 0) { 45 queue.dequeue()(); 46 } 47 })(); 31 if (activeCount < concurrency) { 32 run(fn, resolve, ...args); 33 } else { 34 queue.push(run.bind(null, fn, resolve, ...args)); 35 } 48 36 }; 49 37 50 const generator = (fn, ...args) => new Promise(resolve => { 51 enqueue(fn, resolve, ...args); 52 }); 53 38 const generator = (fn, ...args) => new Promise(resolve => enqueue(fn, resolve, ...args)); 54 39 Object.defineProperties(generator, { 55 40 activeCount: { … … 57 42 }, 58 43 pendingCount: { 59 get: () => queue. size44 get: () => queue.length 60 45 }, 61 46 clearQueue: { 62 47 value: () => { 63 queue. clear();48 queue.length = 0; 64 49 } 65 50 } … … 70 55 71 56 module.exports = pLimit; 57 module.exports.default = pLimit;
Note:
See TracChangeset
for help on using the changeset viewer.