# Asynchronous JavaScript: Callbacks, Promises and async/await

> Handle work that finishes later, from callbacks to promises to async/await, including parallelism, cancellation and error handling. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/frontend/async-javascript
Last updated: 2026-11-28

Handle work that finishes later, from callbacks to promises to async/await, including parallelism, cancellation and error handling.

Intermediate · about 7 hours · difficulty 3/5

Learn first: [Events and Event Delegation](https://www.internhack.in/roadmaps/tracks/frontend/events)

Almost everything interesting on the web is asynchronous: network requests, timers, user input, reading files. JavaScript runs on a single thread, so it cannot wait by blocking; it registers work to finish later.

### Three generations

1. **Callbacks**: pass a function to run when done (`setTimeout(fn, 1000)`). Nesting many leads to "callback hell" and scattered error handling.
2. **Promises**: an object representing a future value that is pending, then fulfilled or rejected. Chain with `.then()`, handle failures with `.catch()`, clean up with `.finally()`.
3. **async/await**: syntax over promises. An `async` function always returns a promise; `await` pauses that function (not the whole page) until the promise settles. Use `try/catch` for errors.

### Combinators

- `Promise.all` runs in parallel and rejects if any fails.
- `Promise.allSettled` waits for all and reports each result.
- `Promise.race` and `Promise.any` take the first to settle or succeed.

### Common mistakes

- Awaiting in a loop when requests are independent. Start them together and `await Promise.all(...)` instead.
- Forgetting `await`, so you get a Promise object instead of data.
- Not returning a promise from a `.then()` callback, breaking the chain.
- Ignoring **cancellation**: use `AbortController` so a stale search request does not overwrite newer results (a race condition).

### Good enough to move on

You can wrap a callback API in a promise, fetch three endpoints in parallel with proper error handling, and cancel an in-flight request when the user types again.

## Key concepts

- **Callbacks and callback hell**
- **Promise states and chaining**
- **async and await**
- **Error handling in async code**
- **Promise.all, allSettled, race, any**
- **Sequential vs parallel awaits**
- **AbortController and cancellation**
- **Promisifying callback APIs**

## Mini project

Build a movie search box that queries a free public API, debounces input, cancels stale requests with AbortController, and shows loading, empty and error states.

## Check yourself

What happens if one of three promises passed to Promise.all rejects, and when would you choose allSettled instead?

## Free resources

- [Promises, async/await](https://javascript.info/async) (javascript.info)
- [Using promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) (MDN)

Previous: [Events and Event Delegation](https://www.internhack.in/roadmaps/tracks/frontend/events) · Next: [The Event Loop](https://www.internhack.in/roadmaps/tracks/frontend/event-loop)

## More in JavaScript Meets the Browser

- [DOM Manipulation](https://www.internhack.in/roadmaps/tracks/frontend/dom)
- [Events and Event Delegation](https://www.internhack.in/roadmaps/tracks/frontend/events)
- [Asynchronous JavaScript: Callbacks, Promises and async/await](https://www.internhack.in/roadmaps/tracks/frontend/async-javascript)
- [The Event Loop](https://www.internhack.in/roadmaps/tracks/frontend/event-loop)
- [Fetch and Working with APIs](https://www.internhack.in/roadmaps/tracks/frontend/fetch-api)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Frontend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/frontend)
