Skip to content
Async JavaScriptChapter 34 / 47Intermediate4 min read

Async / Await

Learn JavaScript async await to write promises as straightforward, readable code, with try catch, parallel await, and top-level await.

On this page

1. What is async / await?

An async function always returns a promise. By writing await inside it, you wait for a promise to settle, and the result lands directly in a variable.

await pauses only that function, not the whole program. The rest of the page and code keeps running.

2. Why does it matter?

Long .then chains get hard to read, especially when there are conditions and loops. async / await lets you write the same logic straight from top to bottom.

then chainasync / await
A then inside a thenStraight lines from top to bottom
Hard to carry variables from one step to the nextNormal const variables
catch is a separate methodThe familiar try catch
Async with loops is hardawait inside for...of is easy

3. Where is it used?

PlaceExample
API callsconst res = await fetch(url)
Form submitMaking the submit handler async to send data
Node.js scriptsReading files, database queries
Next.js / React Server ComponentsFetching data in async components
ES modulesTop-level await for loading config

4. How does it work?

What happens at await
The async function starts running
At await it gets a promise
The function pauses, control goes back to the caller
The promise settles
The rest of the function resumes as a microtask
If the promise isWhat await does
FulfilledReturns the value
RejectedThrows the error, which try / catch catches

5. Code example

Basic async function with try catch

js
async function loadUser(id) {
  try {
    const res = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    const user = await res.json();
    return user.name;
  } catch (error) {
    console.error("Load failed:", error.message);
    return null;
  } finally {
    console.log("Request finished");
  }
}

const name = await loadUser(1);
console.log(name);

Sequential vs parallel

js
const slowA = () => new Promise((r) => setTimeout(() => r("A"), 1000));
const slowB = () => new Promise((r) => setTimeout(() => r("B"), 1000));

async function sequential() {
  const a = await slowA();
  const b = await slowB();
  return [a, b];
}

async function parallel() {
  const [a, b] = await Promise.all([slowA(), slowB()]);
  return [a, b];
}

How does the code work?

LineWhat is happening
async function loadUserThe function now returns a promise
await fetch(...)The function waited until the response arrived
if (!res.ok) throwTurned the HTTP error into a real error
catch (error)A network or thrown error lands here
sequential()About 2 seconds, one after the other
parallel()About 1 second, both together

Interview tip

A value returned from an async function is also wrapped in a promise. So the result of loadUser(1) is a Promise, not a string, and it needs await or .then.

6. Common mistakes

Mistake: Forgetting await

Why? The variable gets a pending Promise instead of the data, and user.name is undefined.

Better: Put await in front of every call that returns a promise when you need the result.

Mistake: await inside forEach

Why? forEach does not wait for an async callback, and the code moves on.

Better: Use a for...of loop if you need order, or Promise.all with map if you want parallel.

Mistake: Writing independent awaits in a line

Why? Every request waits for the previous one, and the page gets slow.

Better: Run tasks that do not depend on each other inside Promise.all.

Mistake: await without try catch

Why? A rejected promise becomes an unhandled error.

Better: Keep await inside try catch, or add a catch on the caller.

7. What to learn before and after

Suggested order
Callbacks
Promises
Async Await
Fetch API
Error Handling

Final quick revision

PointDetail
asyncThe function always returns a promise
awaitExtracts the promise's result, pauses only that function
ErrorsA rejected promise becomes a throw, catch it with try / catch
Parallelawait Promise.all([...])
Loopsawait with for...of, not with forEach
Top-level awaitWorks in ES modules without a function