Reputation: 163
I'm reading files with Node in a 4 step process. Each step has to wait for the resolution of the function it calls. It seems like a very simple task to accomplish, but the async functions are not awaiting the result of their variable definitions ( i.e. even though the variable 'finish' is dependent on the variable processData which is awaiting an async function, it fires anyway.
I'm guessing I have a butchered understanding of the fundamental behavior if my conclusions aren't correct. And I know there are a ton of threads with this issue, but I would appreciate some help. Oh, and if I log the results from the fs.readFile method in its callback, the data shows up. But the data is returned undefined from that block. Code below:
async function formatData(){
const processData = await getFiles()
const finish = await printData(processData)
}
async function getFiles(){
const checkFile = await getFileData('./csvs/checkfile_for_node_test.csv')
const scheduleFile = await getFileData('./csvs/schedules_for_node_test.csv')
console.log('returning files')
return {checks: checkFile, schedules: schedFile}
}
async function getFileData(file){
const fileData = await fs.readFile(file, (err, data) => data )
console.log(`file data: ${fileData}`)
return fileData
}
function printData(data){
console.log(data)
return data
}
formatData()
Upvotes: 0
Views: 1772
Reputation: 174
I believe you are missing promise from getFileData function. Below is the modified code
const fs = require("fs");
async function formatData() {
const processData = await getFiles();
const finish = await printData(processData);
console.log(finish);
}
async function getFiles() {
const checkFile = await getFileData("./hello.txt");
const scheduleFile = await getFileData("./world.txt");
console.log("returning files");
return { checks: checkFile, schedules: scheduleFile };
}
async function getFileData(file) {
return new Promise((res, rej) => {
fs.readFile(file, (err, data) => {
if (err) rej(err);
console.log(`file data: ${data}`);
res({ fileData: data });
});
});
return fileData;
}
async function printData(data) {
console.log(data);
return data;
}
formatData();
Upvotes: 2
Reputation: 904
I don't know if the code you provided is the actual code, but there seems to be a lot of issues. I will break down them one by one for you:
When you are calling formatData
you should use await
keyword before it because it is an async
method.
At the last line of getFiles
method, you mistakenly placed schedFile
instead of scheduleFile
.
You are trying to use fs.readFile
with await. It does not work that way. You have to either promisify it or use readFileSync
.
All put together in code:
async function formatData(){
const processData = await getFiles()
const finish = await printData(processData)
}
async function getFiles(){
const checkFile = await getFileData('./csvs/checkfile_for_node_test.csv')
const scheduleFile = await getFileData('./csvs/schedules_for_node_test.csv')
console.log('returning files')
return {checks: checkFile, schedules: scheduleFile}
}
async function getFileData(file){
const readFileAsync = promisify(fs.readFile);
const fileData = await readFileAsync(file, { encoding: 'utf-8' });
console.log(`file data: ${fileData}`)
return fileData
}
function printData(data){
console.log(data)
return data
}
window.addEventListener('load', async function () {
await formatData();
});
I hope it works.
Upvotes: 1