Sending consecutive sms messages with Twilio the second message arrives before the first

127 views Asked by At

I have an issue when trying to send 2 consecutive messages via nodejs teilio

My sendMessage function looks like this

client.messages.create({
        body: msg,
        to: toNumber,
        from: keys.TWILIO_PHONE_NUMBER,
    })
    .then(async (message) => {
        res.send("Ok, good job");
    });

And I am using it like this

sendChatMessage(
    "Message body to send",
    req.body.From,
    res
);

And then

sendChatMessage(
    "Follow up message body to send",
    req.body.From,
    res
);

However the follow up message arrives before the first message I tried putting the 2nd message in a timeout for a few seconds but it doesn't feel like the right solution.

1

There are 1 answers

0
Deadron On

You are not showing us enough of your code but it looks like you may not actually waiting for your async calls to succeed before sending the second call? You need to make sure you are awaiting async calls and or returning promises and participating in the promise chains.