Skip to main content
TOPIC 6 OF 8 · 2 MIN READ

Async/Await

The async and await keywords simplify handling Promises, allowing asynchronous code to appear synchronous.

Key concepts

async

Makes a function return a Promise.

await

Pauses the execution until the Promise resolves or rejects. Can only be used inside async functions.

Step by step

How It Works:

  • The await keyword pauses the execution until the Promise resolves or rejects.
  • The try-catch block handles errors gracefully.

Examples

Async/Await Example:

async function fetchData() {
  try {
    const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error('Fetch failed:', error);
  }
}
fetchData();

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus