Intermediate level · Module 2: Asynchronous JavaScript · Lesson 3 of 3
async/await and Error Handling
Write asynchronous code that reads like synchronous code with async/await, handle errors with try/catch and run independent work in parallel.
In this lesson you will
- Write async functions with await and try/catch
- Avoid sequential awaits for independent work
Key concepts
async functionawaittry/catch/finallyunhandled rejection
The lesson
An async function always returns a promise. Inside it, await pauses the function until a promise settles and gives you its value, so code reads top to bottom. Errors from awaited promises can be caught with ordinary try/catch.
Forgetting await is a common bug: the variable then holds a promise, not the value. Forgetting to handle errors leads to unhandled promise rejections, which browsers and Node.js report as errors.
Awaiting independent operations one after another is slow. Start them together and await them together: const [rates, prices] = await Promise.all([getRates(), getPrices()]);.
Step by step
- Rewrite a promise chain as an async function.
- Add try/catch/finally around it.
- Show the forgotten-await bug and fix it.
- Run two independent calls in parallel with Promise.all.
Practice activity
Fix a provided async function that loads data sequentially, ignores errors and sometimes returns a promise instead of a value.
Expected result: The fixed function runs calls in parallel, reports errors clearly and always returns real values.
Check your understanding
-
What does an async function return?
Show answer
A promise.
-
How do you run two independent awaits in parallel?
Show answer
Start both and await Promise.all of them.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.