0

How can I run an async func in a setInterval like in the example below?

 setInterval(async () => {  
            
  }, millisPerHour);
Gianmarco
  • 792
  • 2
  • 14
  • 38
  • 1
    Yes, this is the way you should run it. – micronyks Jul 25 '20 at 08:22
  • Possible duplicate of https://stackoverflow.com/questions/52184291/async-await-with-setinterval – Sergey Khmelevskoy Jul 25 '20 at 08:27
  • What have you tried? What was the result? Did you get any errors/Problems? – Thomas Jul 25 '20 at 09:06
  • I tried and seems not working honestly – Gianmarco Jul 25 '20 at 09:30
  • In what way was it not working? Can you describe the exact problem you had? What was your code, what did you expect to happen, and what happened instead? It would appear the problem was located elsewhere. – CherryDT Jul 25 '20 at 19:31
  • actually I had a problem, I also made another question https://stackoverflow.com/questions/63069640/console-log-value-in-the-on-ready-sequelize-do-not-change and I tried to async a setInterval but probalby this is not the problem – Gianmarco Jul 25 '20 at 19:36

1 Answers1

2

try with this code
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

function resolveAfter2Seconds() {
      return new Promise(resolve => {
        setTimeout(() => {
          resolve('resolved');
        }, 2000);
      });
    }
    
    async function asyncCall() {
      console.log('calling');
      const result = await resolveAfter2Seconds();
      console.log(result);
      // expected output: "resolved"
    }
    
    asyncCall();`
Murosadatul
  • 116
  • 6