Node.js async.queue() Method
Last Updated :
11 Jun, 2021
The async module is is designed for working with asynchronous JavaScript in NodeJS. The async.queue returns a queue object which is capable of concurrent processing i.e processing multiple items at a single time.
How to use async.queue?
- Step 1: Create a package.json file. A package.json file is created by the running the following command.
npm init
- Step 2: Installing the async module. The async module can be installed using the following command.
npm i async
- Step 3: Importing the async module. The async module can be imported using the following command.
const async = require('async')
- Step 4: Using the async.queue module. Syntax of async.queue.
const queue = async.queue('function', 'concurrency value')
The parameter function is executed on the element added to the queue. The concurrency value tells the queue, the number of elements to be processed at a particular time.
Example: Have a look at the below example for better understanding.
Javascript
const queue = async.queue((task, completed) => {
console.log( "Currently Busy Processing Task " + task);
setTimeout(()=>{
const remaining = queue.length();
completed( null , {task, remaining});
}, 1000);
}, 1);
|
Important methods and properties in async.queue:
push(element, callback) :The push method is used to add elements to the tail of the queue. The following code demonstrates how push method works.
-
Javascript
queue.push(item, (error, {item, remaining}) => {
if (error){
console.log(`An error occurred while processing task ${task}`);
} else {
console.log(`Finished processing task ${task}
. ${remaining} tasks remaining`);
}
});
|
-
length(): The length method returns the number of elements currently present in the queue. The following code demonstrates how the length method works.
-
Javascript
console.log(queue.length());
|
-
started property: The started property returns a boolean value, indicating whether the queue has started processing the data or not. The following code demonstrates how the started property works.
-
Javascript
console.log(queue.started)
|
-
unshift(element, callback) :The unshift method is similar to the push method, but the element is added to the head of the queue, indicating that the element to be processed is an important one. The following code demonstrates how the unshift method works :-
-
Javascript
queue.unshift(item, (error, {item, remaining}) => {
if (error){
console.log(`An error occurred while processing task ${task}`);
} else {
console.log(`Finished processing task ${task}. ${remaining} tasks remaining`);
}
});
|
-
drain() Method : The drain method runs a callback function when the queue is done executing all the tasks. The following code demonstrates how the drain method works.
-
Note: The drain method only works when the function described is an arrow function.
-
Javascript
queue.drain(() => {
console.log( 'Successfully processed all items' );
})
|
-
pause() Method : The pause method pauses the execution of elements in the queue until the resume function is called. The following code demonstrates how the pause method works.
-
-
resume() Method: The resume method resumes the execution of elements in the queue. The following code demonstrates how the resume method works.
-
-
kill() Method: The kill method removes all the elements from the queue, the callback function of the drain method and forces it into idle. The following code demonstrates how the kill method works.
-
-
idle() Method: The idle() method return a boolean value, indicating whether the queue is idle or processing something. The following code demonstrates how the idle method works.
-
-
Complete Code: The following code is a complete demonstration of how the async.queue is actually used.
Javascript
const async = require( 'async' );
const tasks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const queue = async.queue((task, completed) => {
console.log( "Currently Busy Processing Task " + task);
setTimeout(()=>{
const remaining = queue.length();
completed( null , {task, remaining});
}, 1000);
}, 1);
console.log(`Did the queue start ? ${queue.started}`)
tasks.forEach((task)=>{
if (task == 5){
queue.unshift(task, (error, {task, remaining})=>{
if (error){
console.log(`An error occurred while processing task ${task}`);
} else {
console.log(`Finished processing task ${task}. ${remaining} tasks remaining`);
}
})
} else {
queue.push(task, (error, {task, remaining})=>{
if (error){
console.log(`An error occurred while processing task ${task}`);
} else {
console.log(`Finished processing task ${task}. ${remaining} tasks remaining`);
}
})
}
});
queue.drain(() => {
console.log( 'Successfully processed all items' );
})
console.log(`Did the queue start ? ${queue.started}`)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...