'Why is my Async Await not working in NodeJS?
Trying to perform a basic async/await in NodeJS but it is still going out of order.
Code I am trying to run:
var fs = require('fs');
var stringData;
async function find(context) {
try {
let text = await readFile('./sql/test.sql', context);
} catch (e) {
console.log('Try/Catch Error: ' + e)
}
console.log('display')
console.log('display ' + JSON.stringify(text))
return
}
async function readFile(file, context) {
new Promise((resolve) => {
fs.readFile(file, (error, data) => {
if (error) {
throw error;
}
stringData = data.toString()
}),
(err) => {
console.log('ERROR: ' + JSON.stringify(err));
}, () => {
resolve(1);
}
})
};
module.exports.find = find;
When I run the above I get an undefined results for the console.log for text.
I am expecting text to use the await to populate before moving down to the console logs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
