const fs = require('fs');
// const util = require('util');
let text = ``;
const files = [`head.txt`,`body.txt`,`leg.txt`,`feet.txt`];
let head,body,leg,feet = ``;
//const readFile = util.promisify(fs.readFile);
function isCompleted(err,data) {
if (err) {
throw err;
}
text = data.toString();
proceedData()
}
function proceedData() {
console.log(text);
return text;
}
function readFileBody() {
fs.readFile(files[1], 'utf8', isCompleted);
}
async function printFiles () {
//head = await fs.readFile(files[0], 'utf8', (err,data) => {if (err) {throw err;}else {head += ` ${data}`}});
body = await readFileBody();
//await fs.readFile(files[2], 'utf8', isCompleted);
//await fs.readFile(files[3], 'utf8', isCompleted);
}
printFiles();
When I console.log(body) in the global scope, it will return an undefined.
When I console.log(body) in the local printFile function (without return), it will return an undefined too, but I console.log(text) in local isCompleted, it will return a value of text file.
How do I store a value of text file to the variable?