This project is a simple Express API that demonstrates two different ways to handle asynchronous code when working with Mongoose:
- Promise style —
.then().catch()callbacks - async/await style — cleaner, more readable syntax
Perfect for beginners who want to see both styles side-by-side!
- Connects to MongoDB (via Mongoose)
- Defines a
Userschema withnameandemail - Exposes API routes to:
- Create a user
- Get all users
- Each action is implemented twice:
- Promise style (
/users/promise) - async/await style (
/users/async)
- Promise style (
- Create User →
POST /users/promise - Get All Users →
GET /users/promise
// Example POST body:
{
"name": "John Doe",
"email": "[email protected]"
}-Create User → POST /users/async -Get All Users → GET /users/async
// Example POST body:
{
"name": "Jane Doe",
"email": "[email protected]"
}- Promise style
User.find()
.then(users => res.json(users))
.catch(err => res.status(500).send(err));-Looks like a chain of .then() and .catch()
-
Can get messy if you have multiple async steps (callback hell but with nicer syntax)
-
Async/Await style
try {
const users = await User.find();
res.json(users);
} catch (err) {
res.status(500).send(err);
}- Reads top-to-bottom like normal code
- Easier to follow for complex logic = Still uses Promises under the hood — await just unwraps them
- Both styles work the same — it’s all Promises behind the scenes.
- Promise style is fine for short chains, but can become harder to read.
- Async/Await makes async code look synchronous and is easier to debug.
- In production, always wrap async code in try/catch to handle errors.