0
I would like to know if it is possible to receive the return of a Promise using async await?
I created a file where I read a file and return the string of that file, to receive this variable, in my other function I created a function ASYNC await, but apparently it is not waiting for this action to be executed to give the console.log() and that makes my variable come Undefined, follow the code.
APP.JS
const ManagerFile = require('./ManagerFile');
var managerFile = new ManagerFile();
async function readFile() {
var result = await managerFile.Read('./users.csv');
console.log(result)
}
readFile();
Managerfile.js
const fs = require('fs');
const util = require('util');
class ManagerFile {
constructor() {
this.readFile = util.promisify(fs.readFile);
}
Read(pathFile) {
this.readFile(pathFile, { encoding: 'utf-8' }).then((data, err)=>{
return data;
});
}
}
module.exports = ManagerFile;
Problem
console.log(result) - UNDEFINED
In the method
.Read()
, missing areturn
before thethis.readFile
.– Gustavo Sampaio
Unfortunately I still return Undefined
– Bruno Bafilli