Answer the question
In order to leave comments, you need to log in
How to make a function run asynchronously?
I want to move part of the logic into a separate function, but the main code does not want to wait for the file to be read... I tried several ways, but none of them work...
module.exports = async function(path) {
await fs.readFile(`./server/db/${path}.json`, 'utf-8', (err, data) => {
if (!err) {
return data;
}
});
}
server.get('/catalog', (req, res) => {
reader('catalog')
.then(data => {
res.send(data);
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question