×

Node.js async.queue() Method

Node.js async.queue() Method

What is Async module?

In Node.js, we have an async module that has a lot of functionality, but the main use case of this module is to do multiple processes at a continuous time.

To execute multiple tasks at once, this method uses a queue. A queue is a data structure which works in the order of First In First Out, which means whatever element is added first to the queue will be executed first although we can change the order of elements according to our priority.

To use the async module in our file, first, we have to install it using npm(Node Package Manager).

Syntax:

npm install async

After installing, we will have to import this module into our file.

Syntax:

const async=require('async');

As we know, we always use the require keyword to import any module in our file in Node.js. Now, the next step is to initialize the queue where we will execute our tasks.

Syntax:

Const queue=async.queue('fxn', 'concurrency_val');

If you notice the above syntax, then we see that async.queue() method accepts two arguments.

The first one is a function and the second one is the concurrency value.

Fxn is the function which will be executed upon the elements which have been added to the queue by the user. Concurrency value is an integer argument which means the number of tasks executing at a time.

For example:

// Defining The queue
const que = async.queue((element, completed) => {	
	console.log("Currently running task is the task number " + element);
		setTimeout(()=>{
		const remaining_task = queue.length();
		completed(null, {element, remaining_task});
	}, 2000);


}, 1);

Explanation:

In the above code, we use the concurrency value as 1, which means we will process one task at a time.

In the function, we defined that on each element, we will print the element which is under process, and then we will call the timeout function after 2000 milliseconds or 2 seconds. We use the remaining_task variable to store the remaining task in the queue, which is going to be executed next.

We have a lot of functions which can be used on our async queue.

1. length() method

This method returns an integer variable which describes the number of elements present in the queue.

So, with this function, we can determine the number of remaining tasks.

Syntax:

qu.length();

2. push() method

The push method takes two arguments: the first one is the element or task which we want to add to the queue, and the other one is a callback function which will be executed when we process that task.

One thing to notice is that in the push method, tasks are added to the end of the queue.

Syntax:

qu.push(task, (err, {task, remaining}) => {
if(err){
	console.log(`there is an error  in the task ${task}`);
} else {
	console.log(`queue has execute the task ${task}
			. ${remaining} tasks remaining`);
}
});

Started attribute

This attribute returns a Boolean value. If the queue has started the execution of tasks, then it will return true else, it will return false.

Syntax:

qu.started;

3. unshift() method

It is exactly the same as the push method, but the key difference is that it adds the task in front of the queue instead of ending. We use this function if we want to execute some important tasks first.

Syntax:

qu.unshift(task, (err, {task, remaining}) => {
if(err){
	console.log(`there is an error  in the task ${task}`);
} else {
	console.log(`queue has execute the task ${task}
			. ${remaining} tasks remaining`);
}
});

4. drain() method

This method basically runs a callback function which will be executed when we have zero elements in the queue or all the tasks have been completed.

Syntax:

qu.drain();

5. pause() method

This function will pause the execution of tasks in the queue.

Syntax:

qu.pause();

6. resume() method

This function will start the execution of tasks if execution is paused using the pause() method.

Syntax:

qu.resume();

7. kill() method

This function will remove the tasks from the queue, and it will not run the drain() method also.

Basically, it will put the queue into the idle condition where no task is executed.

Syntax:

qu.kill();

8. idle() method

This function will return a boolean value. If the queue is not doing any task, then it will return true else, it will return false.

Syntax:

qu.idle();


Related Topics

Angular 8 *ngFor Directive

Angular 8ngFor Directive The *ngFor directive is used to repeat to repeat a portion of HTML template once per each item from an iterable list (collection). The ngFor is an Angular...

2 minutes read.

Angular 8 Tutorial

What is Angular 8 Angular is a Framework of JavaScript used to build web and mobile applications. Angular 8 is a client-side TypeScript based structure which is used to create dynamic...

6 minutes read.

Angular 8 Conclusion

Angular version 8 looks like a more accessible solution entirely focused on the modern technological trends added features. Like Ivy, route configurations use dynamic imports, builder APIs in the CLI, web worker support,...

2 minutes read.

Difference between Angular and React

Difference between Angular and React Parameters AngularReactType Angular is a complete framework. React is a JavaScript library, and much older compared...

2 minutes read.

Express.js app.get() Request Function

Express.js Express is a flexible online application framework for Node.js that offers a strong set of functionality for both web and mobile applications. While preserving the familiar and adored Node.js features,...

4 minutes read.

String Interpolation in Angular 8

String Interpolation in Angular 8 String interpolation is a one-way data-binding technique which is used to output the data from a typescript code to HTML template. It uses the template expression. It uses the...

2 minutes read.

Angular 8 ngClass Directive

Angular 8 ngClass Directive ngClass Directive is a type of attribute directive. Angular provided built-in directive, and it helps in adding or removing CSS classes on an HTML element. The ngClass directive allows...

2 minutes read.

Angular 8 Error Fixing

We can get errors in Angular because of many causes. Let's see an example to see some specific types of errors. We have to create an app with the name "testing-app." In this app,...

4 minutes read.

Angular 8 Unit Testing

What is unit testing? Unit testing is a type of software testing where individual components of the software are tested. It is done during the development of any application. A unit may be...

6 minutes read.

Angular 8 Observables

Observables provide support for passing messages between publisher and subscription in our application. The observables can deliver the multiple values of any type like literal, messages, depending on the content. It is an...

6 minutes read.

Event Binding in Angular 8

Event binding in Angular 8 Event binding is a data-binding from an element to a component. User actions such as keystrokes, mouse movement, clicks, and touches can be bound to the component property...

1 minute read.

ngSwitch Directive in Angular 8

Angular 8 ng-Switch Directive The ng-Switch Directive hides and shows the HTML elements depending on an expression. Child elements with the ng-switch-when directive will be displayed if it gets a match; otherwise, the component and...

2 minutes read.

Angular 8 ngIf Directive

ngIf Directive is the part of structural directives.The NgIf is the most straightforward structural Directive and comfortable to understand. The ngIf Directives is used to add and remove HTML elements according to...

2 minutes read.

Deployment of an Angular 8 app

Deploying an Angular 8 app To implement our application, we have to compile it and then host the JavaScript, CSS, and HTML on a web server. Build an Angular application which is very portable...

4 minutes read.

Scalability and Validation Forms in Angular 8

Form Validation Validation is an essential part of managing any set of forms. If we are checking for required fields or querying an external API for a username. Angular 8 provides a set of...

1 minute read.

Data Binding in Angular 8

What is Binding? Binding is the process which generates the connection between the application UI and the data which comes from the business logic. In Angular, it is called the automatic synchronization of...

3 minutes read.

Creating first APP in Angular 8

Firstly, we have to open Git Bash and, then we have to write the following command in it. ng new my -app The project has been created now so that now we have to go...

2 minutes read.

Dependency Injection in Angular 8

Dependency injection (DI), is an essential application design pattern. Angular 8 has its own DI framework, which used in the design of Angular application to increase efficiency and portability. Dependencies are the services that...

7 minutes read.

Angular 8 with Bootstrap

How to install Bootstrap for Angular? Run the following Command in Command prompt. npm install–save bootstrap@3= > The @3 is essential! After that, when we use a project created with Angular CLI 6+, we will...

1 minute read.

Difference between Node.js and ASP.net

Introduction to Node.js V8 (the JavaScript engine operating inside Google Chrome) is packaged with certain libraries and is mostly used for input & output, which includes creating data and managing connection...

4 minutes read.