async.queue() Method in Node.js


The async module provides different functionalities to work with asynchronous JavaScript in a nodejs application. The async.queue() method returns a queue that is further used for concurrent processing of processes i.e. multiple processing of items at a time/instant.

Installing and Using async.queue()

Step 1 − Run the following command to initialize the node package manager.

npm init

Step 2 − Installing the async module using the following command.

npm install --save async

Step 3 − Importing the async module using the below statement in your programs .

const async = require('async')

Syntax

async.queue('function', 'concurrency value')

Parameters

The above parameters are described as below −

  • function – This parameter defines the function that will be executed over the element added to the queue.

  • concurrency value – This field defines the number of elements to be processed at a time.

The async.queue() method further has multiple methods and properties that will be used while processing async requests.

  • push(element, callback) - Similar to an ordinary queue, the push method is used for adding an element at the tail of a queue.

queue.push(item, callback);
  • length() - The length method is used for returning the number of elements present in a queue at a time.

queue.length()
  • started property - This property returns a boolean value providing information about the queue whether it has started processing its elements or not.

queue.started()
  • unshift(element, callback) - The unshift property also adds an element to the queue like a push() method. The only difference between the two is – It adds elements at the head whereas push adds it in the tail. This method is used for priority elements.

queue.unshift(item, callback)
  • drain() Method - This method issues a callback when the queue has executed all the tasks/elements. It only works when the function is described in an arrow function.

queue.drain(() => {
   console.log(“All Tasks are completely executed...”);
}
  • pause() Method  This method holds the execution of remaining elements in the queue. The function will continue after resume() is called.

queue.pause()
  • resume() Method - This method is used for resuming the execution of elements which were put on hold using the pause() method.

queue.resume()
  • kill() Method - This method removes all the remaining elements from the queue and forces it into an idle state.

queue.kill()
  • idle() Method - This method returns a boolean state indicating if the queue is idle or processing something.

queue.idle

Example

Let's take a look at one example to understand the above concepts better −

// Including the async module
const async = require('async');

// Creating an array for all elements execution
const tasks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Initializing the queue
const queue = async.queue((task, executed) => {
   console.log("Currently Busy Processing Task " + task);

   setTimeout(()=>{
      // Number of tasks remaining and to be processed
      const tasksRemaining = queue.length();
      executed(null, {task, tasksRemaining});
   }, 1000);

}, 1); // concurrency value = 1

// Queue is idle initially as no elements are there...
console.log(`Queue Started ? ${queue.started}`)

// Adding each task from the tasks list
tasks.forEach((task)=>{

   // Adding the task 5 to the head for priority execution
   if(task == 5){
      queue.unshift(task, (error, {task, tasksRemaining})=>{
         if(error){
            console.log(`An error occurred while processing task ${task}`);
         }else {
            console.log(`Finished processing task ${task}. ${tasksRemaining} tasks remaining`);
         }
      })
      // Adding all the tasks at tail to be executed except task 5
   } else {
      queue.push(task, (error, {task, tasksRemaining})=>{
         if(error){
            console.log(`An error occurred while processing task ${task}`);
         }else {
            console.log(`Finished processing task ${task}. ${tasksRemaining}
            tasks remaining`);
         }
      })
   }
});

// Executes the callback when the queue is done processing all the tasks
queue.drain(() => {
   console.log('All items are succesfully processed !');
})

// Checking if the queue is started after adding tasks
console.log(`Queue Started ? ${queue.started}`)

Output

C:\home
ode>> node asyncQueue.js Queue Started ? False Queue Started ? True Currently Busy Processing Task 5 Finished processing task 5. 9 tasks remaining Currently Busy Processing Task 1 Finished processing task 1. 8 tasks remaining Currently Busy Processing Task 2 Finished processing task 2. 7 tasks remaining Currently Busy Processing Task 3 Finished processing task 3. 6 tasks remaining Currently Busy Processing Task 4 Finished processing task 4. 5 tasks remaining Currently Busy Processing Task 6 Finished processing task 6. 4 tasks remaining Currently Busy Processing Task 7 Finished processing task 7. 3 tasks remaining Currently Busy Processing Task 8 Finished processing task 8. 2 tasks remaining Currently Busy Processing Task 9 Finished processing task 9. 1 tasks remaining Currently Busy Processing Task 10 Finished processing task 10. 0 tasks remaining All items are succesfully processed !

Updated on: 20-May-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements