Trying to understand async await

You can only usefully await a promise.

setTimeout returns a timeout id (a number) that you can pass to clearTimeout to cancel it. It doesn’t return a promise.

You could wrap setTimeout in a promise…

async function test() {
  await new Promise( resolve => setTimeout(() => {
    console.log('done');
    resolve("done");
  }, 1000));

  console.log('it finished');
}


test();

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top