I'm working with files and text. I'm using textract
to extract the text out of documents and publishing the text using amqplib
message-broker (if this isn't your thing, still you can answer). I have multiple async/await functions to run. After all the document's text is published, I want to close the amqplib
's connection. Here is the code.
const startExtraction = async (dir, channel, connection) => {
console.log("Started");
const files = fs.readdirSync(dir);
let i=0
for (let file of files){
const native = `${root}\\${file}`;
try {
textract.fromFileWithPath(native, async (err, text) => {
if (err) {
console.log(err);
return;
}
const doc = await File.create(payload); // saving the text to database
channel.sendToQueue(queue, Buffer.from(JSON.stringify(doc)));
console.log("Sent "+ doc._id);
});
}catch(err){
console.log(err);
}finally{
i++;
}
}
console.log("Done");
}
startExtraction is called here
const initiateExtraction = async (job) => {
try {
const conn = await amqp.connect('amqp://localhost')
const channel = await conn.createChannel()
await channel.assertQueue(queue.MLIFY, { durable: true });
await startExtraction(job, channel, conn);
console.log("in then from a top level promise");
// const promise = new Promise(async ()=>{
// await startExtraction(job, channel, conn);
// });
// await promise.then(()=>{ I tried like this(the commented part)
// closeConnection(); // this function closes the connection.
// });
} catch (error) {
console.error(error)
}
}
I'm not sure if I'm close to the solution. I'm having tough time wrapping my head around the asyc/await. The goal of this question is to get a way to execute a function closeConnection()
after the text extraction and publishing to the broker. Thanks in advance.