Node.js async.queue() Method

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
// Defining The queueconst queue = async.queue((task, completed) => {    // Here task is the current element being    // processed and completed is the callback function         console.log("Currently Busy Processing Task " + task);         // Simulating a complex process.    setTimeout(()=>{        // Number of elements to be processed.        const remaining = queue.length();        completed(null, {task, remaining});    }, 1000); }, 1); // The concurrency value is set to one,// Which means that one element is being// Processed at a particular time
 
 
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
// Takes in two parameters, the item being pushed and// the callback function// Here the item is the element being added// and other is the callback function with error// and data property(destructured) 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
// Returns true if the queue has started processing the data else falseconsole.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
// Takes in two parameters, the item being pushed// and the callback function// Here the item is the element being added// and other is the callback function with error// and data property(destructured) 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
// Executes when the queue is done processing all the itemsqueue.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.
 
 
Javascript
// Pauses the execution of the queuequeue.pause()
  
  
resume() Method: The resume method resumes the execution of elements in the queue. The following code demonstrates how the resume method works.
 
 
Javascript
// Resumes the execution of the queuequeue.resume()
  
  
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.
 
 
Javascript
// Forces the queue into idle mode// and removes the drain callbackqueue.kill()
  
  
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.
 
 
Javascript
// Returns whether the queue is idle or notqueue.idle()
  
  
 
Complete Code: The following code is a complete demonstration of how the async.queue is actually used.
 
Javascript
// Importing the async moduleconst async = require('async'); // Creating a tasks arrayconst tasks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Defining the queueconst queue = async.queue((task, completed) => {    console.log("Currently Busy Processing Task " + task);         // Simulating a Complex task    setTimeout(()=>{        // The number of tasks to be processed        const remaining = queue.length();        completed(null, {task, remaining});    }, 1000); }, 1); // The concurrency value is 1  // The queue is idle as there are no elements// for the queue to processconsole.log(`Did the queue start ? ${queue.started}`) // Adding the each task to the queuetasks.forEach((task)=>{     // Adding the 5th task to the head of the    // queue as it is deemed important by us 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`);      }    })             // Adding the task to the tail in the order of their appearance } 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`);      }      })    }});  // Executes the callback when the queue is done processing all the tasksqueue.drain(() => {    console.log('Successfully processed all items');}) // The queue is not idle it is processing the tasks asynchronouslyconsole.log(`Did the queue start ? ${queue.started}`)
 
 
Output:
 
 
anikakapoor
Node.js-Methods
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
JWT Authentication with Node.js
Installation of Node.js on Windows
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
Mongoose find() Function
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React