Day 87 of 100daysofcode : Why You Need Two Awaits for API Calls
Today I learned why two await keywords are crucial when working with async API calls! Here’s why:
First await : Resolves the HTTP request (gets response headers)
Second await : Parses the response body as JSON (can be slow/large)
Real-World Analogy
It’s like ordering coffee:
- Wait for your order to be taken (first await)
- Wait again for the coffee to be made (second await)
Skipping the second wait = getting an empty cup!
Key Takeaways:
fetch() returns a Promise for the network request
.json() returns another Promise for body parsing
Async operations need to be fully awaited at every stage
Always add error handling with try/catch around your awaits!
lebanon-mug