README.md
md3 kBIn Examples.zip
Chapter 3: Asynchronous Programming
This folder contains examples for understanding asynchronous programming in Node.js.
📚 Learning Order
Follow these files in order:
1. async-apis.js - Start Here! ⭐
What you'll learn:
- What operations are asynchronous in Node.js
- Why async operations don't block your code
- Common async APIs:
setTimeout(),fs.readFile(),fs.writeFile(),fetch(), database queries
Run:
node async-apis.jsKey Concepts:
- Asynchronous operations run without blocking
- Results come back through callbacks or Promises
- Node.js can handle many operations simultaneously
2. promises.js - Understanding Promises
What you'll learn:
- What is a Promise?
- How to create and use Promises
.then()and.catch()methods- How async APIs return Promises
- Promise chaining and
Promise.all()
Run:
node promises.jsKey Concepts:
- Promise states: pending, fulfilled, rejected
resolve()andreject()functions- Many async APIs return Promises!
3. async-await.js - Cleaner Promise Syntax
What you'll learn:
asyncandawaitkeywords- How async/await makes Promise code cleaner
- Real-world examples with
fs.promises - Comparison: Promise vs async/await
Run:
node async-await.jsKey Concepts:
asyncmakes a function return a Promiseawaitwaits for a Promise to resolve- Much cleaner than
.then()chains!
4. try-catch.js - Error Handling
What you'll learn:
- How to handle errors with async/await
- Using
try/catchblocks - Best practices for error handling
Run:
node try-catch.jsKey Concepts:
- Always wrap
awaitintry/catch - Errors from rejected Promises go to
catchblock - This is the pattern you'll use in Express routes!
🎯 Quick Reference
Asynchronous Node.js APIs
These operations run without blocking:
setTimeout()- Schedule code to run laterfs.readFile()/fs.writeFile()- File operationsfetch()- HTTP requests- Database queries - MongoDB, MySQL, etc.
- Network operations - Sending/receiving data
Promise Basics
// Creating a Promise
const myPromise = new Promise((resolve, reject) => {
if (success) {
resolve(value); // ✅ Success → goes to .then()
} else {
reject(error); // ❌ Failure → goes to .catch()
}
});
// Using a Promise
myPromise
.then(result => console.log(result))
.catch(error => console.log(error));async/await Pattern
async function main() {
try {
const result = await someAsyncOperation();
console.log(result);
} catch (error) {
console.log(error);
}
}Express Route Pattern (You'll use this everywhere!)
app.get('/users/:id', async (req, res) => {
try {
const user = await User.findById(req.params.id);
res.json(user);
} catch (error) {
res.status(500).json({ error: error.message });
}
});📖 Related Tutorial
See the full tutorial: NodeJS_Tutorial/03-async-programming.html
✅ Checklist
After going through these examples, you should understand:
- What operations are asynchronous in Node.js
- Why async operations don't block your code
- What a Promise is and how it works
- How to create and use Promises
- How to use
async/awaitsyntax - How to handle errors with
try/catch - The Express route pattern with async/await
Happy learning! 🚀