You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-1Lines changed: 30 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -2974,7 +2974,36 @@ module.exports = router
2974
2974
<b><a href="#">↥ back to top</a></b>
2975
2975
</div>
2976
2976
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
+
constfs=require('fs');
2990
+
constdata=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
+
constfs=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
+
<divalign="right">
3004
+
<b><a href="#">↥ back to top</a></b>
3005
+
</div>
3006
+
2978
3007
#### Q. ***What is difference between promise and async await in Node.js?***
2979
3008
#### Q. ***How to use JSON Web Token (JWT) for authentication in Node.js?***
2980
3009
#### Q. ***How to build a microservices architecture with Node.js?***
0 commit comments