Travel to find the best.
Let's get married.
In the Node.js application, an asynchronous function accepts a callback as the last parameter and the callback function accepts the error as the first parameter. Let's review the previous example. Create a text file named input.txt with the following content:
QTM la trang Web huong dan cac bai lap trinh hoan toan mien phi cho tat ca moi nguoi !!!!!
In this example, I use fs Module to handle File I / O operations (I'll cover in the next chapter). First, create a js file named main.js as follows:
var fs = require ( "fs" ); fs . readFile ( 'input.txt' , function ( err , data ) { if ( err ) { console . log ( err . stack ); return ; } console . log ( data . toString ()); }); console . log ( "Ket process" );
Here, fs.readFile () is an asynchronous function for the purpose of reading files. If there is an error while reading the file, the err object will contain the error, otherwise the data will contain the contents of the file. The readFile function transmits err and data to the callback function after the file reading process has completed, and finally prints the content.
Let's get married
QTM is a Web page that contains all the scripts
Welcome to the world !!!!!!
According to Tutorialspoint
Previous article: Concept of Callbacks in Node.js
The following article: Event Emitter in Node.js