JS Guide
HomeQuestionsSearchResources
Search

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext
javascript
mid
async

How do async/await work and how do they relate to Promises?

async-await
promises
asynchronous
error-handling
es2017
Quick Answer

async/await is syntactic sugar over Promises. An async function always returns a Promise, and await pauses execution until a Promise resolves, making asynchronous code look and behave more like synchronous code.

Detailed Explanation

async keyword:

  • Declares a function as asynchronous
  • The function automatically returns a Promise
  • Non-Promise return values are wrapped in Promise.resolve()

await keyword:

  • Can only be used inside async functions (or top-level in modules)
  • Pauses execution until the Promise settles
  • Returns the resolved value or throws on rejection
  • Makes async code read sequentially

Error Handling:

  • Use try/catch blocks instead of .catch()
  • Uncaught rejections propagate up as rejected Promises

Best Practices:

  • Use Promise.all() for parallel operations, not sequential awaits
  • Always handle errors with try/catch
  • Avoid mixing .then() and await unless necessary

Code Examples

Basic async/await
// Promise-based
function fetchUser(id) {
  return fetch(`/api/users/${id}`)
    .then(response => response.json())
    .then(user => {
      console.log(user);
      return user;
    });
}

// async/await equivalent
async function fetchUser(id) {
  const response = await fetch(`/api/users/${id}`);
  const user = await response.json();
  console.log(user);
  return user;
}

Resources

MDN - async function

docs

JavaScript.info - Async/await

article

Related Questions

Explain the JavaScript Event Loop and how it handles asynchronous operations.

mid
runtime

What are Promises in JavaScript and how do they work?

mid
async
Previous
What are Promises in JavaScript and how do they work?
Next
Explain the JavaScript Event Loop and how it handles asynchronous operations.