this question has answer here:
- how return response asynchronous call? 21 answers
i'm trying following code work:
'use strict'; var fs = require('fs'); var filename = 'readme.txt'; var str = fs.readfile(filename, 'utf8', function (err, data) { if (err) { console.log(err); throw err; } return data; }); console.log('result read: ' + str);
readme.txt:
console.log('working');
i'm trying display following:
result read: console.log('working');
readfile executed asynchronously, data
can accessed inside callback function, if want synchronous, should use readfilesync
async:
'use strict'; var fs = require('fs'); var filename = 'readme.txt'; var str = fs.readfile(filename, 'utf8', function (err, data) { if (err) { console.log(err); throw err; } console.log('result read: ' + data); });
sync:
var str = fs.readfilesync(filename, 'utf8'); console.log('result read: ' + str);
Comments
Post a Comment