Async/Await – Simplifying Asynchronous JavaScript
October 10, 2017
async
and await
landed in ES2017, letting developers write asynchronous code that reads like synchronous steps.
Before: Promise chain
function fetchUser(id) {
return fetch(`/api/users/${id}`)
.then(res => res.json())
.then(data => {
console.log('User:', data);
})
.catch(err => console.error(err));
}
Nested .then()
calls grow quickly and push error handling to the end.
After: async / await
async function fetchUser(id) {
try {
const res = await fetch(`/api/users/${id}`);
const data = await res.json();
console.log('User:', data);
} catch (err) {
console.error(err);
}
}
How it works
async
marks the function, returning a promise automatically.await
pauses execution until the promise resolves, yielding the result.try/catch
handles both network errors and thrown exceptions.
Sequential vs parallel
// sequential
await stepOne();
await stepTwo();
// parallel
const [a, b] = await Promise.all([stepOne(), stepTwo()]);
Use Promise.all
to start tasks together, then await their combined result.
Tips
- Always wrap awaited code in
try/catch
for proper error handling. - Combine with
Promise.allSettled
for bulk operations where some may fail. - Keep
async
functions small; extract chunks to maintain readability.
Async/await became mainstream in 2017, replacing many callback and promise chains with clear top‑to‑bottom control flow.
Recent posts
- At-Least-Once vs. Exactly-Once - Understanding Message Delivery Guarantees
June 12, 2025
Learn about message delivery guarantees in distributed systems. Understand why most production systems implement at-least-once delivery with idempotency rather than attempting exactly-once delivery.
- How Idempotency Saves Your API from Chaos
June 11, 2025
Learn how to implement idempotency in your APIs to prevent duplicate actions and ensure data consistency. Includes practical examples with Supabase and Node.js.
- Vibe Coding ‑ Notes from the First Try
June 6, 2025
Quick lessons from spinning up a new blog with an AI pair‑programmer and Cursor.