JavaScript Interview Question #82
What is async/await in JavaScript?
Promises, Async/Await, Fetch & Modern JavaScript Mid-Level Intermediate
Quick Interview Answer
async/await is syntax built on top of Promises that allows asynchronous code to be written in a more sequential style. An async function always returns a Promise.
Detailed Explanation
async/await is syntax built on top of Promises that allows asynchronous code to be written in a more sequential style. An async function always returns a Promise.
await pauses execution of that async function until the awaited value settles, while allowing the JavaScript runtime to continue processing other work. If the awaited Promise rejects, await throws the rejection reason.
Code Example
async function loadUser() {
const response = await fetch("/api/users/1");
const user = await response.json();
return user;
}
loadUser().then(user => console.log(user));