Non-Blocking

This overview will refer to the event loop and libuv but no prior knowledge of those topics is required. Readers are assumed to have a basic understanding of the JavaScript language and Node.js callback pattern.
Post Reply
aryatechno
Site Admin
Posts: 9
Joined: Tue Sep 26, 2023 9:01 am

Non-Blocking

Post by aryatechno »

All of the I/O methods in the Node.js standard library provide asynchronous versions, which are non-blocking, and accept callback functions. Some methods also have blocking counterparts, which have names that end with Sync.

Blocking methods execute synchronously and non-blocking methods execute asynchronously.

Using the File System module as an example, this is a synchronous file read:

const fs = require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent asynchronous example:

const fs = require('fs');
fs.readFile('/file.md', (err, data) => {
if (err) throw err;
});
Post Reply