Skip to content

Commit 2ed21f8

Browse files
committed
Blocking Code
1 parent 4738438 commit 2ed21f8

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2974,7 +2974,36 @@ module.exports = router
29742974
<b><a href="#">↥ back to top</a></b>
29752975
</div>
29762976

2977-
#### Q. ***How Node prevents blocking code?***
2977+
#### Q. ***How node.js prevents blocking code?***
2978+
2979+
**Blocking vs Non-blocking**
2980+
2981+
**Blocking** is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes. This happens because the event loop is unable to continue running JavaScript while a **blocking** operation is occurring.
2982+
2983+
Synchronous methods in the Node.js standard library that use **libuv** are the most commonly used blocking operations. Native modules may also have blocking methods. Blocking methods execute `synchronously` and non-blocking methods execute `asynchronously`.
2984+
2985+
*Example:*
2986+
2987+
```js
2988+
// Blocking
2989+
const fs = require('fs');
2990+
const data = fs.readFileSync('/file.md'); // blocks here until file is read
2991+
console.log(data);
2992+
moreWork(); // will run after console.log
2993+
2994+
// Non-blocking
2995+
const fs = require('fs');
2996+
fs.readFile('/file.md', (err, data) => {
2997+
if (err) throw err;
2998+
console.log(data);
2999+
});
3000+
moreWork(); // will run before console.log
3001+
```
3002+
3003+
<div align="right">
3004+
<b><a href="#">↥ back to top</a></b>
3005+
</div>
3006+
29783007
#### Q. ***What is difference between promise and async await in Node.js?***
29793008
#### Q. ***How to use JSON Web Token (JWT) for authentication in Node.js?***
29803009
#### Q. ***How to build a microservices architecture with Node.js?***

0 commit comments

Comments
 (0)