Currently I am trying to have an async function which has another function inside like this:
processFile: async function(req, whatTo, callback){
const lines = [];
const lineReader = require('readline').createInterface({
input: require('streamifier').createReadStream(req.file.buffer)
});
let errorPresent = false;
lineReader.on('line', line => {
lines.push(line.replace(/ /g,''));
});
lineReader.on('close', async () => {
try {
const results = await Promise.map(lines, async(line) => {
return await Transform(line);
}, { concurrency: 80 });
return results.join("\r\n");
}catch(err){
throw err;
}
});
}
Then I have a route, which calls this function, like this:
const data = await tokenizer.processFile(req, 'tokenize');
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Disposition', 'attachment; filename=tokenized.txt');
res.write(data, 'binary');
res.end();
The return results.join("\r\n"); isn't 'returning' the processFile.
How can I achieve this?
processFiledoesn't actually return anything and doesn't really wait to return, theasyncis superfluous. Also, the mixing of eventful and asynchronous code doesn't make sense either. You're mixing callbacks, async/await, and explicit Promises in a way that doesn't make sense.lineReader.on('close', async () ...? I did mention I'm new to Node?