I was trying to read a JSON
file using Async/Await, I created my example based Native async/await approach, and I got this error.
SyntaxError: await is only valid in async function
Here is my code.
const fs = require('fs-extra');
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
const path = "file.json";
function parseJM() {
return new Promise(function (resolve, reject) {
fs.readFile(path, { encoding: 'utf-8'}, (err, data) => {
if (err) { reject(err); }
else {
resolve (parser.parseString(data.replace(/<ent_seq>[0-9]*<\/ent_seq>/g, "")
.replace(/&(?!(?:apos|quot|[gl]t|amp);|#)/g, '')));
}
});
});
}
const var1 = await parseJM();
console.log(var1);
What is wrong with my code? My node version is 11.9.0, my npm version is 6.7.0 and i am using Arch Linux.