async await

-async and await make promises easier to write async: async makes a function return a Promise await: await makes a function wait for a Promise

by Kabir Hossain

JavaScript

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