Node.js: Asynchronous & Synchronous Code Programming

Node.js is asynchronous in nature. The Node.js APIs are asynchronous or non-blocking. The long-running operations/APIs in Node.js are non-blocking.

In Node.js:

– The server never waits for any operation to complete and return the data.
– The server moves to the next API/operation after calling it.
– The response of the previous API call is returned via Event notification system in Node.js.

If our code waits for any operation (e.g. input/output operation) to complete and only then executes itself then it is said to be blocking or synchronous in nature. Input/Output (I/O) operation can be like reading from or writing to a file or database.

By default, all the I/O operations in Node.js are asynchronous/non-blocking in nature. However, some methods in Node.js have blocking/synchronous counterparts with the name ending with Sync.

Asynchronous File Read

Let’s take an example of reading a file in Node.js. We will create a file named app.js.

app.js


const fs = require('fs');

console.log('Start reading file.');

fs.readFile('myfile.txt', 'utf-8', function(err, content) {
    if (err) {
        console.log(err);       
        throw err;
    }
    
    console.log(content);       
});

console.log('End reading file.');

Run the application


node app.js

Output:


mukesh:nodejs chapagain$ node app.js
Start reading file.
End reading file.
{ Error: ENOENT: no such file or directory, open 'myfile.txt'
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'myfile.txt' }

/Users/mukeshchapagain/Documents/nodejs/app.js:8
        throw err;
        ^

Error: ENOENT: no such file or directory, open 'myfile.txt'

We get the error message as an output because we have not yet created the file myfile.txt.

– The first error message is from the console.log(err) code.
– The second error message is from throw err code.

So, let’s create a file named myfile.txt and add some text content to it.


echo "This is a sample test file." >> myfile.txt

Now, when we again run the application:


node app.js

Output:


mukesh:nodejs chapagain$ node app.js
Start reading file.
End reading file.
This is a sample test file.

The content of the file myfile.txt is displayed as output.

Note that:

– Both the file reading start and end messages are displayed before the content of the myfile.txt file.
– This happens because Node.js is asynchronous/non-blocking in nature.
– The application does not wait for the completion of file read and moves on to the next line of code and executes it.
– It takes some fraction of time to read the file. Hence, at first the code below it got executed and then only the file read output is printed.

Synchronous File Read

There’s a counterpart/alternative function for synchronous/blocking file read in Node.js. The function name is readFileSync().


const fs = require('fs');

console.log('Start reading file.');

const content = fs.readFileSync('myfile.txt', 'utf-8'); 
console.log(content);

console.log('End reading file.');

Output:


mukesh:nodejs chapagain$ node app.js
Start reading file.
This is a sample test file.
End reading file.

Here, the application will wait until the readFileSync() function completes reading the file. Only then, the program moves to the next line of code. Hence, there is a sequential printing of message in the output.