I would like to send messages every 5 seconds on differents numbers with this code:
const send_message = ["34123456789","34123456789","34123456789"];
client.on("ready", () => {
console.log("Client is ready!");
send_message.map((value) => {
const chatId = value + "@c.us";
const message = 'This is a test message';
client.sendMessage(chatId, message);
});
});
Where or how can I add this code to create a correct bucle?
await sleep(5000);
2
Answers
use a for…of loop instead and make the client.on callback
async
Something like
You can use
await
only in an async context. So you can do like this for instance, usingfor .. of
instead ofmap
BTW since node 16 you don’t need to create your own
sleep
(or similar) method anymore. You can usesetTimeout
from thetimers/promises
module