优先级队列(PriorityQueue)是个很有用的数据结构,很多编程语言都有实现。NodeJs是一个比较新潮的服务器语言,貌似还没有提供相关类。这些天有用到优先级队列,因为时间很充足,闲来无事,就自己实现了一下。代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...
importPQueuefrom'p-queue';importgotfrom'got';constqueue=newPQueue({concurrency:1});(async()=>{awaitqueue.add(()=>got('https://sindresorhus.com'));console.log('Done: sindresorhus.com');})();(async()=>{awaitqueue.add(()=>got('https://avajs.dev'));console.log('Done: avajs.d...
importPQueuefrom"p-queue";importgotfrom"got";constqueue=newPQueue({concurrency:1});(async()=>{awaitqueue.add(()=>got("https://sindresorhus.com"));console.log("Done: sindresorhus.com");})();(async()=>{awaitqueue.add(()=>got("https://avajs.dev"));console.log("Done: avajs.d...
257 ```js 258 import delay from 'delay'; 259 import PQueue from 'p-queue'; 260 261 const queue = new PQueue({concurrency: 2}); 262 263 let count = 0; 264 queue.on('active', () => { 265 console.log(`Working on item #${++count}. Size: ${queue.size} Pending...
$ node example.js 1. Added 🦄 2. Added 🐴 3. Queue size: 1 4. Pending promises: 1 5. Resolved 🦄 6. Resolved 🐴 7. Queue is empty 8. Pending promises: 0 9. Added 🐙 10. Pending promises: 1 11. Resolved 🐙 12. All work is done ...
$ node example.js 1. Added 🦄 2. Added 🐴 3. Queue size: 1 4. Pending promises: 1 5. Resolved 🦄 6. Resolved 🐴 7. Queue is empty 8. Pending promises: 0 9. Added 🐙 10. Pending promises: 1 11. Resolved 🐙 12. All work is done ...
log("Done: avajs.dev"); })(); (async () => { const task = await getUnicornTask(); await queue.add(task); console.log("Done: Unicorn task"); })(); API PQueue(options?) Returns a new queue instance, which is an EventEmitter3 subclass. options Type: object concurrency Type: ...
如图ArrayBlockingQueue内部有个数组items用来存放队列元素,putindex下标标示入队元素下标,takeIndex是出队...
10 10 import dayjs, { Dayjs } from 'dayjs' 11 11 import fs from 'fs-extra' 12 - import pLimit from 'p-limit' 13 12 import md5 from 'md5' 14 13 import FileType from 'file-type' 15 14 import { QBittorrent } from '@cao-mei-you-ren/qbittorrent' ...
Doing some research, it seems that p-queue is different from Sindre's other p-* modules in that p-queue has a build step and exports ./dist/index.js: p-queue/package.json Line 9 in c12ec2e "exports": "./dist/index.js", Meanwhile the other packages don't have a build step...