Introducing delay between messages

  javascript, mqtt, node.js

I use Node.js. I have an MQTT message event handler

client.on('message', function (topic, message) {
  // calls another function

which calls another function my_function on receiving messages.

async function my_function(topic,message) {
   const value = await dataFromPLC();
///processes the value together with message

The function dataFromPLC exported from another file using exports.dataFromPLC = dataFromPLC and imported into my main function looks like this

let client = new S7Client(plcSettings);
client.on('error', console.error);
async function dataFromPLC (){
  try {
    await client.connect();
  } catch (err){
  try {
    // Read DB
    const res = await client.readDB(dbNr, dbVars);
    return res;
  } catch (err) {
  } finally {

There is no issue when I receive a single MQTT message or there is sufficient delay between messages. However when I receive two MQTT messages, both of them calls my_function and subsequently dataFromPLC without much delay in between. I receive an error as there is not sufficient time for the PLC connection to close before the second message tries to use the connection again. I have looked at different options and am not quite sure about how to solve the problem. Can I get some help please?

Source: Ask Javascript Questions