Toggle navigation
Genres
Frontend (2)
JavaScript (6)
Database (2)
Linux Server (3)
Web Apps (4)
Misc (4)
Search
List
New Tutorials
Last Modified Tutorials
</>
Code examples
code
smiko
javascript
nodejs
fs-sync2.js
fs-sync2.js
/* This example shows synchronous execution, blocking code. console.log(contents); blocks process and that's why console.log('Do something else!'); must wait Max file that didn't crash nodeJS had 350MB */ //load modules var fs = require('fs'); //synchronous file reading var content = fs.readFile('./testfiles/largefile350mb.txt', 'utf8'); console.log(content); //returns 'undefined' because readFile requires callback and dont return any value //here process is blocked while file is not read completely. After that next console.log(); continue console.log('Do something else!'); /* Conclusion: Due to asynchronous execution, first is file dumped then appear 'Do something else'. That's because console.log('Do something else!'); waits fs.readFileSync('largefile350mb.txt', 'utf8'); to be finished. */
Reload page
Preview
W3C validation
Edit Code
JS Console