The modern way to write async code
async/await is syntactic sugar on top of Promises. It lets you write async code that reads top-to-bottom, just like synchronous code. This is what you’ll use 90% of the time.
asyncgoes before the function declaration — marks it as asynchronousawaitgoes before a Promise — pauses execution until the Promise resolves
async functions
Addingasync to a function does one thing: it makes the function always return a Promise.
await inside it must be marked async.
Arrow function version
await — pausing until the Promise resolves
await pauses the function until the Promise settles. The function doesn’t block the page — it just pauses internally while other code keeps running.
Error handling with try/catch
Usetry/catch to handle errors in async functions — just like you would in Python:
try/catch catches both:
- Network errors — when
fetchitself fails (no internet, DNS failure) - Errors you throw — like when
response.okis false
try/catch/finally
Sequential vs parallel
Sequential — one after another
When each request depends on the previous result:Parallel — all at once
When requests are independent, usePromise.all() with await:
Comparing to Python
- JavaScript
- Python
The pattern you’ll use everywhere
This is the complete async/await pattern for API calls:What’s next?
You understand async/await. Now let’s put it to work — making real HTTP requests to your FastAPI backend withfetch.
Fetching data from APIs
Make GET, POST, PUT, and DELETE requests with fetch