Intermediate Topic 18 of 51 Web Development
Asynchronous JavaScript
Non-blocking code using callbacks, promises and async/await.
How it works
Asynchronous programming lets JavaScript start long-running operations such as network requests, timers and file reads without blocking the single main thread. Early code used callbacks, which could nest deeply, then promises chained results with then and catch, and async and await let asynchronous code read like sequential code using promises underneath. The event loop runs callbacks when operations complete, and microtasks run before the next task.
Step-by-step walkthrough
- Slow tasks such as network calls should not freeze the page.
- The call starts and returns immediately.
- The browser does other work meanwhile.
- When the result is ready it is queued.
- Your callback or the code after await runs.
Where it is used
Fetching data, animations, timers, reading files in Node.js and coordinating several requests in parallel with Promise.all use asynchronous patterns.
Common pitfall
Forgetting await returns a promise instead of a value, and unhandled rejections silently fail or crash. Running awaits in a loop serializes requests that could run in parallel. Race conditions arise when responses arrive out of order. Wrap awaits in try and catch, cancel obsolete requests and avoid mixing styles carelessly.
Where it fits in the course
Asynchronous JavaScript is topic 18 of 51 in Web Development, rated Intermediate. It follows Browser DevTools. The next topic is Promises.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Web Development quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Web Development quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Asynchronous JavaScript?
Where do people use Asynchronous JavaScript?
What is the most common mistake with Asynchronous JavaScript?
What should I learn before this topic?
How can I practice it?
Is Asynchronous JavaScript hard to learn?
How long does it take to learn Asynchronous JavaScript?
Which topics are related to Asynchronous JavaScript?
What should I study after Asynchronous JavaScript?
Related topics
- Browser DevTools (Web Development)
- Promises (Web Development)
- JSON (Web Development)
- Event Loop (Web Development)