Async code that reads top to bottom
The async await syntax is sugar over promises. Inside an async function, await pauses until a promise settles, then yields its value as if it returned normally. The function does not block the thread. Under the hood the runtime suspends the function and resumes it when the awaited promise settles.
Errors become exceptions
The big win is error handling. A rejected promise that you await throws an exception at the await point. That means you can wrap async steps in the familiar try and catch.
- try holds the sequence of awaited steps.
- catch receives any rejection as a thrown error.
- finally runs cleanup whether or not an error occurred.
This unifies synchronous and asynchronous error handling under one mechanism.
Pitfalls to avoid
A few traps catch newcomers.
- Forgetting await leaves you holding a pending promise instead of its value.
- Awaiting in a loop when steps are independent serializes work that could run together.
- Unhandled rejections occur when no try or catch surrounds an awaited call.
Run independent work concurrently by starting promises first, then awaiting them together.
Key idea
Async await turns promises into linear code where await yields values and rejections throw, letting try catch and finally handle async errors just like synchronous ones.