# task-queue-ts **Repository Path**: Luomenghao/task-queue-ts ## Basic Information - **Project Name**: task-queue-ts - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-10-16 - **Last Updated**: 2023-10-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 任务队列 ### 一、功能介绍 控制并发、每次最多执行10个,有序执行,执行完毕后有对应的事件,单组任务可通过Promise使用 ### 二、如何使用 #### 1、安装依赖 ```javascript // npm npm install task-queue -S; // yarn yarn add task-queue -S ``` #### 2、使用 ```javascript import { TaskQueue } from 'task-queue'; const t1 = new TaskQueue({ maxLen: 5 }); // 创建一个队列实例 /******************** 基础使用 ********************/ const updateUser = (d) => { const randomTime = Math.floor(Math.random() * 1000 + 1000); return ((timer, data) => { return new Promise(resolve => { setTimeout(() => { resolve(data) }, timer); }) }).bind(null, randomTime, d) } // 任务列表 const updateUserTasks = [updateUser('张三'), updateUser('李四'), updateUser('王五')]; /** * 当前任务加入队列中 * updateUserTasks第一个参数为任务列表 * updateUser 第二个参数为任务的分租id */ t1.push(updateUserTasks, 'updateUser').then(data => { console.log('当前id的对列结束:', data); // ["张三", "李四", "王五"] }); /******************** 其他用法 ********************/ // 全局监听任务完成的事件 t1.on("finish", taskRes => { console.log(taskRes); // { id: 'xxx', result: []} }); // 全局监听pending事件 t1.on("pending", taskRes => { console.log(taskRes); // { id: 'xxx', result: []} }); ``` ### 三、方法说明 | 方法 | 参数 | 说明 | | ----- | ------------------------------- | ----------------------------------- | | on | on(key: string, (data) => {}) | 监听事件,可用:`finish` `pending` | | push | push([], 'taskId') | 添加任务 | | reset | reset() | 重置任务 |