Callbacks → Promises → async/await Evolution
Async programming in Node.js evolved through three generations. Understanding each generation — and why the next one was invented — gives you a mental model for reading old code and writing clean modern code.
Three Generations Side by Side
import { readFile } from 'fs';
import { readFile as readFileAsync } from 'fs/promises';
// ━━ Generation 1: Callbacks (Node.js error-first convention) ━━
// Problem: Callback Hell — pyramid of doom once you chain 3+ async ops
readFile('./a.txt', 'utf8', (errA, dataA) => {
if (errA) return console.error(errA);
readFile('./b.txt', 'utf8', (errB, dataB) => {
if (errB) return console.error(errB);
// → Keep nesting deeper and deeper...
});
});
// ━━ Generation 2: Promises ━━
// Flat chaining, single .catch() for the whole chain
readFileAsync('./a.txt', 'utf8')
.then(dataA => readFileAsync('./b.txt', 'utf8'))
.then(dataB => console.log(dataB))
.catch(err => console.error(err)); // one handler for all errors
// ━━ Generation 3: async/await ━━
// Reads like synchronous code, still non-blocking
async function loadFiles(): Promise<string> {
const dataA = await readFileAsync('./a.txt', 'utf8');
const dataB = await readFileAsync('./b.txt', 'utf8');
return dataA + dataB;
}
// Error handling with async/await:
try {
const content = await loadFiles();
console.log(content);
} catch (err) {
console.error('Failed to load files:', err);
}
// Converting callbacks to promises: util.promisify
import { promisify } from 'util';
const readFileP = promisify(readFile);
const data = await readFileP('./a.txt', 'utf8');Tip
Tip
Practice Callbacks Promises asyncawait Evolution in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Async code runs non-blocking — callbacks, promises, and async/await
Practice Task
Note
Practice Task — (1) Write a working example of Callbacks Promises asyncawait Evolution from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Callbacks Promises asyncawait Evolution is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready node code.