Node JS with sync and async call
suppose Node JS have readFile which is great example of async call , Node JS have other function readFileSync which is example of Sync call .
var fs =require('fs');
var crypto = require('crypto');
var file=fs.readFileSync('abc.txt');
crypto.createHash('md5').update(file).digest('hex')
//other code
var file=fs.readFileSync('abc.txt'); is sync call means it not run other bellowed code untill it get done with readFile , which is good in our case because in the next time using file content which is danger if it not fully read.
if we use readFile than it async call .With the asynchronous methods there is no guaranteed execution ordering So need to provide callback function which will be called when function done with read file
var fs =require('fs');
var crypto = require('crypto');
fs.readFile('abc.txt','utf-8',function(err,data){
if(err) throw err;
crypto.createHash('md5').update(data).digest('hex')
});
//other code
what if do not use call back function to collect content.
var fs =require('fs');
var crypto = require('crypto');
var content=fs.readFile('abc.txt','utf-8');
console.log(content);//undefined
file content is undefined in that case.