DEV Community

Mohd Ahshan Danish
Mohd Ahshan Danish

Posted on

Solution for 'await is only valid in async functions'

We can only execute await inside an async function. If you try to use await without an async function, it will result in an error.

code

Solution: One good option to address this issue is by using IIFE (Immediately Invoked Function Expression).

(async function(){
    let a = await testSleep();
    console.log(a);
   let b = await testSleep();
    console.log(b);
})();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)