- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Understanding blocking and unblocking of code execution in Node
Now, we have a file writing function writeFileSync in fs module as shown below −
const requestBody = []; req.on('data', (chunks)=>{ requestBody.push(chunks); }); return req.on('end', ()=>{ const parsedData = Buffer.concat(requestBody).toString(); const username = parsedData.split('=')[1]; fs.writeFileSync('username.txt', username); //redirect res.statusCode=302; res.setHeader('Location','/'); return res.end(); });
Sync means synchronized. It’s a blocking code example. Once file write is completed then only code execution for rest of the file starts. Above code is simpler but if we have a large file handling operation it will result into slow performance of app.
This way of code execution will slow down the other requests and eventually performance of application.
Solution to this problem is to use asynchronous function of file writing. We have writeFile in fs module which is asynchronous. Below is the example for it −
return req.on('end', ()=>{ const parsedData = Buffer.concat(requestBody).toString(); const username = parsedData.split('=')[1]; fs.writeFile('username.txt', username, (error)=>{ console.log(error); }); //redirect res.statusCode=302; res.setHeader('Location','/'); return res.end(); });
writeFile takes one more argument i.e. error object. If there is any error in completing the file handling function we can check it with error in console.
This way we can avoid the code blocking. In file handling Node js can use the operating systems multithreading approach to give better performance. It just registers the events and executes them later at specific points.
Asynchronous is most used way of handling operations in node.js like fetching data from server , manipulating the data , pagination etc.
readFile and readFileSync −
similar to write file functions we have read files functions which are also categorized into blocking and non-blocking way.
readFile is non-blocking and readFileSync is blocking. We can use promises to handle operations which are yet to complete but will respond with some resolve once done .
PromiseOperationFunction() .then( ()=> console.log(‘completed with succes’); ) .catch((err)=> console.log(‘completed with failure’); );
- Related Articles
- Understanding the Event driven code execution approach in Node
- Understanding the http requests in Node
- Understanding the Node lifecycle and event loop in node.js
- Understanding Code Reuse and Modularity in Python 3
- Understanding the npm - Node module system
- What is Blocking Networks and Non-Blocking Networks in Computer Architecture?
- Measure execution time of small Python code snippets (timeit)
- How to calculate Execution Time of a Code Snippet in C++?
- How to get the timing Execution Speed of Python Code?
- How to identify blocked and blocking sessions in Oracle ?
- Are jQuery events blocking?
- What is ad blocking?
- First and last child node of a specific node in JavaScript?
- Microsoft Emergency Path for RCE (Remote Code Execution) in Windows Malware Scanner (CVE-2017-0290)
- Understanding components and props in React.js
