17.4 Homework Assignment (15 mins)
Objective:
Students will apply the concepts of asynchronous programming, including callbacks, promises, and async/await, to simulate real-world tasks. This assignment reinforces the topics covered in the lecture and programming activities.
Assignment Task:
Create a JavaScript program that simulates the following sequence of asynchronous tasks:
Fetch user profile (Simulate fetching a user profile with a delay).
Fetch user posts (Simulate fetching a user's posts after the profile is fetched).
Fetch user comments (Simulate fetching comments for a specific post).
Implement the solution using:
Callbacks
Promises
Async/Await
Step-by-Step Procedure for Programming Activity
1. Setup the Asynchronous Tasks
Simulate the following functions using setTimeout:
fetchUserProfile: Resolves after 1 second with a user object.fetchUserPosts: Resolves after 2 seconds with an array of posts for the user.fetchPostComments: Resolves after 1.5 seconds with an array of comments for a post.
function fetchUserProfile(callback) {
setTimeout(() => {
callback(null, { id: 1, name: 'Alice' });
}, 1000);
}
function fetchUserPosts(userId, callback) {
setTimeout(() => {
if (userId !== 1) {
callback('User not found', null);
} else {
callback(null, ['Post 1', 'Post 2']);
}
}, 2000);
}
function fetchPostComments(post, callback) {
setTimeout(() => {
callback(null, [`Comment for ${post}`]);
}, 1500);
}2. Task 1: Implement Using Callbacks
Use nested callbacks to execute the tasks sequentially:
Fetch the user profile.
Fetch posts for the user.
Fetch comments for the first post.
Solution:
fetchUserProfile((err, user) => {
if (err) {
console.error(err);
return;
}
console.log(`User: ${user.name}`);
fetchUserPosts(user.id, (err, posts) => {
if (err) {
console.error(err);
return;
}
console.log(`Posts: ${posts}`);
fetchPostComments(posts[0], (err, comments) => {
if (err) {
console.error(err);
return;
}
console.log(`Comments for ${posts[0]}: ${comments}`);
});
});
});3. Task 2: Refactor Using Promises
Modify the task functions to return promises instead of accepting callbacks:
function fetchUserProfile() {
return new Promise((resolve) => {
setTimeout(() => resolve({ id: 1, name: 'Alice' }), 1000);
});
}
function fetchUserPosts(userId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (userId !== 1) {
reject('User not found');
} else {
resolve(['Post 1', 'Post 2']);
}
}, 2000);
});
}
function fetchPostComments(post) {
return new Promise((resolve) => {
setTimeout(() => resolve([`Comment for ${post}`]), 1500);
});
}Solution Using Promises:
fetchUserProfile()
.then((user) => {
console.log(`User: ${user.name}`);
return fetchUserPosts(user.id);
})
.then((posts) => {
console.log(`Posts: ${posts}`);
return fetchPostComments(posts[0]);
})
.then((comments) => {
console.log(`Comments for Post 1: ${comments}`);
})
.catch((err) => {
console.error(err);
});4. Task 3: Refactor Using Async/Await
Use async/await to make the program more readable:
Create an
asyncfunction to execute the tasks sequentially.Use
try...catchfor error handling.
Solution Using Async/Await:
async function displayUserData() {
try {
const user = await fetchUserProfile();
console.log(`User: ${user.name}`);
const posts = await fetchUserPosts(user.id);
console.log(`Posts: ${posts}`);
const comments = await fetchPostComments(posts[0]);
console.log(`Comments for Post 1: ${comments}`);
} catch (err) {
console.error(err);
}
}
displayUserData();Assignment Deliverables:
A single JavaScript file (
asynchronous.js) containing:The callback-based implementation.
The promise-based implementation.
The async/await implementation.
Expected Outcomes:
Students will learn to handle asynchronous tasks sequentially using different techniques.
They will compare and contrast callbacks, promises, and async/await in terms of readability and error handling.
Gain hands-on experience in refactoring code for improved maintainability.
Last updated