Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i prevent a function from yielding the entire script?

I am new to javascript. I cant find a way to do this anywhere.

I am expecting this:

function sendInChannel(channel, msg, MessageCount){
  for (let i = 0; i < MessageCount; i++) {
    channel.send(msg)
  } 
}

to not pause the entire script. This is a discord.js function, and i want it to send multiple messages at once instead of this function waiting about 0.1 seconds and yielding it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use Promises and async/await to make it asynchronous:

async function sendInChannel(channel, msg, messageCount) {
  for (let i = 0; i < messageCount; i++) {
    await channel.send(msg);
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading