Page 1 of 1

Non-Blocking

Posted: Tue Sep 26, 2023 10:43 am
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;
});