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()
awaitonprintDatawhich is not anasyncfunction. Please make it async and check. You can also simply remove async from printData call.readFile? It doesn't look that way, in which caseawait fs.readFile(...)won't do what you hope.file data: undefinedin your logs, right?