JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Promises & Async/Await

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/awaitJavaScript
// 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;
}

Real-World Applications

Use Cases

Sequential API Calls

Fetching dependent data where each call needs the previous result, such as auth token then user profile then settings

Error Recovery

Using try/catch for granular error handling in complex async workflows with fallback strategies

Batch Processing

Processing arrays of items with controlled concurrency using async iterators or Promise.all with chunking

Mini Projects

Async Data Pipeline

beginner

Build a pipeline that fetches, transforms, and saves data using async/await with error handling at each stage

Rate-limited API Client

intermediate

Build a client that processes queued API requests with configurable concurrency limits using async/await

Industry Examples

Next.js

Server components and route handlers use async/await for data fetching at the framework level

Express

Async route handlers with express-async-errors for automatic error forwarding to error-handling middleware

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.
PrevNext