What is Chaining in Node.js ?
Last Updated :
21 Jul, 2020
Chaining in Node.js can be achieved using the async npm module. In order to install the async module, we need to run the following script in our directory:
npm init
npm i async
There are two most commonly used methods for chaining functions provided by the async module:
- parallel(tasks, callback): The tasks is a collection of functions that runs parallel in practice through I/O switching. If any function in the collection tasks returns an error, the callback function is fired. Once all the functions are completed, the data is passed to the callback function as an array. The callback function is optional.
- series(tasks, callback): Each function in tasks run only after the previous function is completed. If any of the functions throw an error, the subsequent functions are not executed and the callback is fired with an error value. On completion of tasks, the data is passed into the callback function as an array.
Example 1: Parallel Chaining
Filename: index.js
javascript
const async = require('async');
async.parallel([
(callback) => {
setTimeout(() => {
console.log('This is the first function');
callback(null, 1);
}, 500);
},
(callback) => {
console.log('This is the second function');
callback(null, 2);
}
], (err, results) => {
if (err) console.error(err);
console.log(results);
});
Run
index.js file using the following command:
node index.js
Output:
This is the second function
This is the first function
[ 1, 2 ]
Explanation: It is noted that the two functions were executed asynchronously. Hence the output from the second function was received before the first function finished executing due to the setTimeout() method. However, the data is only passed to the callback once both the functions finish execution.
Example 2: Series Chaining
Filename: index.js
javascript
const async = require('async');
async.series([
(callback) => {
setTimeout(() => {
console.log('This is the first function');
callback(null, 1);
}, 500);
},
(callback) => {
console.log('This is the second function');
callback(null, 2);
}
], (err, results) => {
if (err) console.error(err);
console.log(results);
});
Run
index.js file using the following command:
node index.js
Output:
This is the first function
This is the second function
[ 1, 2 ]
Explanation: Unlike the parallel execution, in this case, the second function wasn't executed until the first function completed its own execution. Similar to the parallel method, the results are only passed to the callback function once all the functions in the tasks collection finish execution.
Similar Reads
What is Chunk in Node.js ? In Node.js, the term "chunk" refers to a small, manageable piece of data that is part of a larger dataset. Node.js processes data in chunks, especially when dealing with streams, which makes handling large files or data sources more efficient and memory-friendly. This article explores what chunks ar
4 min read
What is Clustering in Node? Clustering in Node refers to a technique used to enhance the performance and scalability of NodeJS applications by utilizing the capabilities of multi-core systems. With clustering, you can create multiple instances of the NodeJS process, known as workers, each running on a separate CPU core. By dis
2 min read
What is callback hell in Node.js ? To know what is callback hell, we have to start with Synchronous and Asynchronous Javascript. What is Synchronous Javascript? In Synchronous Javascript, when we run the code, the result is returned as soon as the browser can do. Only one operation can happen at a time because it is single-threaded.
3 min read
What is spawn in Node JS ? Node JS is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of an internet browser. In this article, we will learn about the Spawn in NodeJs. PrerequisitesNodeJS fundamentalsAsynchronous ProgrammingChild ProcessesSpawn i
3 min read
What is Piping in Node.js ? Piping in NodeJS is the process by which byte data from one stream is sent to another stream. It is a powerful feature in Node.js that allows data to be transferred from one stream to another seamlessly. Streams are an integral part of Node.js, providing a mechanism for handling data that is too lar
6 min read
What is a callback function in Node? In the context of NodeJS, a callback function is a function that is passed as an argument to another function and is executed after the completion of a specific task or operation. Callbacks are fundamental to the asynchronous nature of NodeJS, allowing for non-blocking operations and enabling effici
2 min read