await keyword js code example

Example 1: await js

// Just watch here https://youtu.be/V_Kr9OSfDeU
// You'll understand in like 7 mins.

Example 2: how to make an async function

function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

//async function:
async function asyncCall() {
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result);
  // expected output: 'resolved'
}

asyncCall();

Example 3: await javascript

If the value of the expression following the await operator is not a Promise, it's converted to a resolved Promise.