I am trying to read asynchronously from a file by using node.js. Here is the code that I use:
var fs = require('fs');
fs.readFile('C:\\Users\\aonal\\Desktop\\gelenhamveri.txt', 'utf8', function(err, contents) {
console.log(contents);
//return contents // didn't work. I tried to return it to a variable.
});
console.log('after calling readFile');
console.log(contents); //empty. I want to use it here.
I can't reach the contents outside of function. I tried to return it or assign it to a global variable but it didn't work. My final purpose is to export all the code as a function then use it in different module like this:
module.exports = {
readFile : function(){//Same as above...}
}
So I need to return the contents. My question is how to return a variable from it to use outside of anonymous function?