limited-concurrent-task-queue
v1.0.8
Published
Queue similar to promise-all and promise-all settled with concurrency control
Downloads
52
Readme
Accepted params - (concurrency, [taskArr]?)
Usage :
const Queue = require('limited-concurrent-task-queue');
async function usage1(){
//Creates queue with concurrency of 1 function/task
const queue = new Queue(1);
//Add multiple functions/tasks to the queue
for(let i=0;i<10;i++>){
queue.add(exampleAsyncFunction);
}
// same as Promise.all()
const result1 = await queue.all();
//same as Promise.allSettled()
const result2 = await queue.allSettled();
//or listen to completion
queue.on('done',(tasksResult)=>{
console.log(tasksResult)
});
//listen to error
queue.on('error',(err)=>{
console.log(err)
});
//flush resources after usage (optional).
queue.flush();
}
async function usage2(){
//Array of tasks
const taskArr = [];
//Add multiple functions/tasks to be given to the queue
for(let i=0;i<10;i++){
taskArr.push(exampleAsyncFunction);
}
//create queue with maximum concurrency as 1 , with the tasks as second param
const queue = new Queue(1,taskArr);
// same as Promise.all()
const result1 = await queue.all();
//same as Promise.allSettled()
const result2 = await queue.allSettled();
//or listen to completion
queue.on('done',(tasksResult)=>{
console.log(tasksResult)
});
//listen to error
queue.on('error',(err)=>{
console.log(err)
});
//flush resources after usage (optional).
queue.flush();
}
//example async function/task
function exampleAsyncFunction(){
let promise = new Promise((res,rej)=>{
setTimeout(()=>{
res('done')},1000)
})
return promise;
}