Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Promises for cleaner async
📖 Scenario: You are building a small Node.js app that fetches user data and their posts from a server. Instead of using nested callbacks, you want to use Promises to keep your code clean and easy to read.
🎯 Goal: Build a simple Node.js script that uses Promises to fetch user data and posts asynchronously, then logs the combined result.
📋 What You'll Learn
Create a function that returns a Promise resolving user data
Create a function that returns a Promise resolving posts data
Use a configuration variable to simulate a delay time
Chain Promises to fetch user data first, then posts
Log the combined user and posts data after both Promises resolve
💡 Why This Matters
🌍 Real World
Using Promises is common in Node.js apps to handle asynchronous tasks like fetching data from APIs or databases without blocking the program.
💼 Career
Understanding Promises is essential for backend developers working with Node.js to write clean, maintainable asynchronous code.
Progress0 / 4 steps
1
Create user data fetch function
Create a function called fetchUser that returns a Promise resolving to the object { id: 1, name: 'Alice' } after a delay of 100 milliseconds.
Node.js
Hint
Use new Promise and setTimeout to simulate async fetching.
2
Add delay configuration variable
Create a constant called delay and set it to 150 to use as the delay time for fetching posts.
Node.js
Hint
Use const delay = 150; to set the delay time.
3
Create posts data fetch function using delay
Create a function called fetchPosts that returns a Promise resolving to the array [{ id: 101, title: 'Post 1' }, { id: 102, title: 'Post 2' }] after a delay using the delay constant.
Node.js
Hint
Use the delay constant inside setTimeout for the posts fetch delay.
4
Chain Promises and log combined data
Use fetchUser() and chain it with fetchPosts() using .then(). Inside the second .then(), log an object with keys user and posts containing the resolved data.
Node.js
Hint
Chain fetchUser() and fetchPosts() with .then() and combine results in an object.
Practice
(1/5)
1. What is the main purpose of using Promises in Node.js?
easy
A. To store data permanently on disk
B. To make the program run faster by using multiple CPUs
C. To write synchronous code only
D. To handle asynchronous tasks without freezing the program
Solution
Step 1: Understand asynchronous tasks
Asynchronous tasks take time and can block the program if not handled properly.
Step 2: Role of Promises
Promises allow handling these tasks without freezing the program by running code after the task finishes.
Final Answer:
To handle asynchronous tasks without freezing the program -> Option D
Quick Check:
Promises manage async tasks = A [OK]
Hint: Promises help avoid freezing during slow tasks [OK]
Common Mistakes:
Thinking Promises speed up code execution
Confusing Promises with synchronous code
Believing Promises store data permanently
2. Which of the following is the correct way to create a Promise in Node.js?
easy
A. const p = Promise(() => { resolve(); });
B. const p = new Promise((resolve, reject) => { /* code */ });
C. const p = new Promise(resolve, reject);
D. const p = Promise.new((resolve, reject) => { });
Solution
Step 1: Check Promise constructor syntax
The Promise constructor requires a function with two parameters: resolve and reject.
Step 2: Validate each option
const p = new Promise((resolve, reject) => { /* code */ }); correctly uses new Promise with a function taking resolve and reject.
Final Answer:
const p = new Promise((resolve, reject) => { /* code */ }); -> Option B
Quick Check:
Correct Promise syntax = B [OK]
Hint: Use 'new Promise' with (resolve, reject) function [OK]