I′m trying to read a file and register its hash to a database, after checking if these file already doesn`t exist. For that I have to use a aync function, like this:
async function createHashAndStore(err, data) { if (err) { console.log(`err ${err}`) } const hashFile = sha256(data.toString('utf8')).toString() const afData = new AfData() try { const createAfData = afData .checkIfFileAlreadyExist(hashFile) .then((exists) => { if (!exists) { afData .store({ hash: hashFile, send_date: new Date() }) .then((response) => console.log(response)) .catch((err) => console.log(err)) return true } else { return false } }) .catch((err) => console.log(err)) console.log(`createAfData ${await createAfData}`) return await createAfData } catch (err) { console.log(err) } } const readFileAsync = util.promisify(fs.readFile) const readFileAndStore = () => readFileAsync( Helpers.tmpPath('uploads/' + fileName), 'utf8', createHashAndStore )
1.4m articles
1.4m replys
5 comments
56.9k users