0

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.

Venkatesh Dharavath
  • 500
  • 1
  • 5
  • 18

1 Answers1

0

Well, finally I was able to fix this. You can see what I've changed to make it work as suggested by @hoangdv.

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 {
        const text = await util.promisify(textract.fromFileWithPath)(native); // fetching the text
        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++;
    }
}

I removed the callback and replaced it with a promise and awaited it. Seems like the patterns in the should be consistent.

Here I'm calling both the functions.

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");
        closeConnection();
      } catch (error) {
        console.error(error)
  }
Venkatesh Dharavath
  • 500
  • 1
  • 5
  • 18