Gianmarco
Gianmarco

Reputation: 842

how to run an async func in setInterval in javascript

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

 setInterval(async () => {  
            
  }, millisPerHour);

Upvotes: 0

Views: 305

Answers (1)

Murosadatul
Murosadatul

Reputation: 125

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();`

Upvotes: 2

Related Questions