JavaScript · Async JavaScript

Async and Await

async and await let you write promise-based code that reads top to bottom like ordinary synchronous code.

Intermediate 5 min Lesson 33 of 49 Runnable examples
Before you start:Promises
0/49 in JavaScript
On this page · 5 min · Intermediate

Learning objectives#

  • Declare async functions
  • Pause on promises with await
  • Handle errors with try/catch
  • Avoid accidental sequential waits

Explanation#

An async function always returns a promise. Inside it, await pauses that function until the awaited promise settles, then gives you its value.

If the awaited promise rejects, await throws, so you handle errors with an ordinary try/catch.

await only pauses the current async function, not the whole program. Other code — click handlers, timers — keeps running.

Visual explanation#

A promise starts pending and settles once: fulfilled with a value or rejected with an errorpendingnew Promise(…)resolve(value)reject(error)fulfilled ✓rejected ✕.then(value).catch(error).finally() runseither way
await unwraps a fulfilled promise; a rejected promise makes await throw into your catch block.

Syntax#

JavaScript Illustrative
async function load() {
  try {
    const value = await somePromise;
  } catch (error) {
    // handle the rejection
  }
}

Code example: Sequential vs parallel awaits#

JavaScriptSequential vs parallel awaits Runnable
const wait = (ms, value) => new Promise(r => setTimeout(() => r(value), ms));


async function sequential() {
  const start = Date.now();
  const a = await wait(300, 'A');
  const b = await wait(300, 'B');
  console.log(a, b, 'sequential ~', Math.round((Date.now() - start) / 100) * 100, 'ms');
}


async function parallel() {
  const start = Date.now();
  const [a, b] = await Promise.all([wait(300, 'A'), wait(300, 'B')]);
  console.log(a, b, 'parallel ~', Math.round((Date.now() - start) / 100) * 100, 'ms');
}


async function main() {
  await sequential();
  await parallel();
  try {
    await Promise.reject(new Error('Network down'));
  } catch (err) {
    console.log('Caught:', err.message);
  }
}
main();
Expected output
A B sequential ~ 600 ms
A B parallel ~ 300 ms
Caught: Network down
Opens this example in the full playground

Step-by-step explanation#

  1. wait() returns a promise that resolves after the given delay.
  2. In sequential(), the second wait does not start until the first finishes — about 600ms total.
  3. In parallel(), both promises are created first and awaited together with Promise.all — about 300ms.
  4. A rejected promise makes await throw, so try/catch catches it like any other error.

Real-world use case#

Loading a dashboard that needs a user profile, notifications and settings: start all three requests and await Promise.all to cut the loading time.

Common mistakes#

await inside forEach

forEach does not wait for async callbacks. Use for…of for sequential work or Promise.all(items.map(...)) for parallel work.

Forgetting await

Without await you get a pending Promise object, not the value.

Best practices#

  • Wrap awaits in try/catch where you can handle the error meaningfully
  • Start independent requests before awaiting them
  • Use for…of with await for ordered, sequential work
  • Check response.ok when using fetch — it does not reject on HTTP errors

Remember this#

async functions always return a promise.

await pauses until the promise settles.

Use try/catch around await for errors.

Practice exercise#

Your task: Rewrite this with async/await: fetchUser(1).then(u => console.log(u.name)).catch(e => console.log(e.message));

Show solution
JavaScript Illustrative
async function showUser() {
  try {
    const user = await fetchUser(1);
    console.log(user.name);
  } catch (e) {
    console.log(e.message);
  }
}
showUser();

Mini quiz#

Async and Await quizMultiple choice Question 1 of 2

What does an async function always return?

Score so far: 0/0

Want more? Practise all JavaScript MCQs · JavaScript interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.