What is async and await in JavaScript?

async before a function makes it return a promise. await, used inside it, waits for a promise and gives its result, without blocking the rest of the program.

const wait = ms => new Promise(r => setTimeout(() => r("ready"), ms));

async function run() {
  console.log("start");
  const result = await wait(300);
  console.log(result);
}
run();
console.log("after run()");

Output:

Tutorials Class - Output Window

start
after run()
ready

Use try...catch for errors. See Async/Await and Fetch.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.