×

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

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.

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

$timeout service in AngularJS

The discipline of web development is expanding quickly. A technology that is released today will inevitably become obsolete in a few months. The webpages were static in the past and...

4 minutes read.

Angular 8 Module

Angular 8 Module It is a collection of services, directives, controllers, filters, and configuration information. angular.module is used to configure the $injector. The module is a container of the different parts of an application....

6 minutes read.

Angular 8 changes and new features

The angular version number indicates the level of changes introduced by the release. The use of semantic versioning helps us understand the potential impact of updating to a new version. Angular is the...

5 minutes read.

Angular 8 Directives

Angular 8 Directives: Directives are instructions in the DOM (Document Object Model). It specifies how to place our business logic in Angular. The directive is markers on a DOM element...

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

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.

What is AOT and JIT Compiler in Angular

A program written in a high-level language is translated into machine code or a low-level language by a piece of software called a compiler. HTML templates are the primary building...

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.

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 Pipes

Angular 8 Pipes Pipes are a useful feature in Angular. These are the simple way to transform values in an Angular template. It takes the integers, strings, array, and dates as input separated with...

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

Two-way Data Binding in Angular 8

Two-way Data Binding in Angular 8 Two-way data binding is a synchronization between the model and the view. We can use the ngModel directive in two-way data binding. Custom two-way data binding is useful...

2 minutes read.

Advantages | Disadvantages of Angular 8

Advantages of Angular 8 There are some Advantages of angular 8 which are given below: It offers clean code developmentHigher PerformanceAn angular framework can take care of routing, which...

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.

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.

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.