×

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

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.

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.

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.

Angular 8 Architecture

Architecture of Angular 8 Angular 8 is a platform and framework which is used to create clients applications in HTML and Typescript. Angular 8 is written in Typescript. Typescript is a...

5 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.

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.

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 Animation

What is Animation? The animation is a process of drawing, designing, making layouts, and preparation of photographic sequences which are integrated into the multimedia and gaming products. A reflection of movement created for displaying...

5 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.

Property binding in Angular 8

Property binding is the primary way to binding data in Angular.  To bind data to a property of any element, we use square braces[ ]. It is also a one-way data-binding...

2 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.

Dynamic components in Angular 8

Dynamic components in Angular 8 The dynamic component is one of the versatile and core concept introduced in Angular, Component template is not fixed. An application needs to load new elements...

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.

How to create a Todo List App in Angular 7

What is a Todo List App? Todo List app is an app where users can add various tasks that they want to perform and have an idea of every task. If...

4 minutes read.

Express.js app.listen() Function

Express or Express.js 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...

3 minutes read.

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...

3 minutes read.

Angular 8 universal

Angular 8 universal Angular Universal is a technology that allows server-side rendering for Angular apps. An angular app is a single-page app, and it runs in a client-side browser. Rendering pages in the DOM is...

6 minutes read.

History and Versions of Angular 8

Introduction to Angular JS Misko created Angular JS and the first version of Angular also name as "Angular 1". He built a framework to handle the downfalls of HTML. The first...

3 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.

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.