English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Node.js callback function: Asynchronicity is one of the basic factors that make Node.js popular. Callback is the implementation of function asynchronicity. Generally, in Node.js, most functions that handle resources have callback variants.
When a resource is called for an asynchronous function for a task, the control is immediately allowed to continue executing the subsequent statements after the function. The task on the resource will start in parallel. This can help Node.js continues to execute other tasks while this function is using the resource. Once the task using the resource is completed, Node.js will use the callback function to resume. Call Callback functions have parameters: data objects, result objects, and/or error objects that contain task-related information.
Blocking function:In contrast to asynchronous functions, synchronous functions will execute blocks until the resource is completed. Therefore, synchronous functions are also called blocking functions.
Node.js nested callback functions : If multiple operations are performed on resources sequentially and must be performed asynchronously, callback functions can be nested.
Now, when reading (task) files (resources), we will see three cases compared with callback functions and blocking functions.
Node.js blocking function example
Node.js callback function example
Node.js Nested Callback Function Example
The following is an example of a Node.js script that synchronously reads the sample.txt file.
var fs = require('fs'); // Read the file sample.txt var data = fs.readFileSync('sample.txt'); console.log("Reading file completed: ") + , new Date().toISOString()); console.log("After readFileSync statement : " + , new Date().toISOString());
When the above program runs
Terminal Output
arjun@arjun-VPCEH26EN:~/nodejs$ node read-file-sync.js Reading file completed: 2017-10-19T12:21:40.103Z After readFileSync statement : 2017-10-19T12:21:40.105Z
The after readFileSync statement always executes after the file reading is completed. fs.readFileSync blocks the execution flow.
The following is an example of a Node.js script, which usesNode.js callback functionAsynchronously read the sample.txt file.
var fs = require('fs'); // Read the file sample.txt fs.readFile('sample.txt', // Callback function called when the file reading is completed function(err, data) { if (err) throw err; // Data is a buffer that contains the content of the file console.log("Reading file completed: ") + , new Date().toISOString()); }); console.log("After readFile asynchronously: "); + , new Date().toISOString());
When the above program runs
Terminal Output
arjun@arjun-VPCEH26EN:~/nodejs$ node read-file-async.js After readFile asynchronously: 2017-10-19T12:25:36.982Z Reading file completed: 2017-10-19T12:25:36.987Z
You may notice that even after executing console.log in the asynchronous statement of readFile, it takes about5Milliseconds. file. fs.readFile('sample.txt', callback function{..}) does not block the execution but starts a new process in parallel with the main control flow to read the file (performing a task on resources).
To demonstrate nested callback functions in Node.js, we will consider the plan of renaming a file and then using an asynchronous function to delete it.
var fs = require('fs'); fs.rename('sample.txt', 'sample_old.txt', // First Callback Function function (err) { if (err) throw err; console.log('File Renamed.'); fs.unlink('sample_old.txt', // Second Callback Function function (err) { if (err) throw err; console.log('File Deleted.'); } ); } );
When the above Node.js example is run with node
Terminal Output
arjun@arjun-VPCEH26EN:~/nodejs$ node nodejs-nested-callback.js File Renamed. File Deleted.
In this Node.js tutorial – Node.js Callback Functions, we learned about the execution flow of callback functions and how they achieve non-blocking, as well as how to combine nested callbacks with examples.