0

I have global variable result_crnt. I try to update it inside of consumerRun function:

global.result_crnt = '';

async function consumerRun(){
    try{ 
        await consumer.run({
            eachMessage: async({ topic, partition, message }) => {
                console.log({
                    topic: topic, 
                    partition: partition,
                    message: message.value.toString(), 
                })

                result_crnt = message.value.toString();
                
            }
            
        })

    }catch (error){
        console.log('ERROR: ', error);
    }
}

When I try to send a response using this variable (result_crnt):

api.get("/test/req", async function(req, res){

    consumerRun();
    while(true){
        if (result_crnt != '') break;
    }
    res.status(200).json({"result_crnt": result_crnt});

})

But it stuck inside while block, and result_crnt doesn't seem to update, even though I get the message inside run (I can see output in console). What am I doing wrong? How can I fix it?

Sara
  • 41
  • 4

0 Answers0