- All exercises use real APIs
- Test your code in the browser console
- Use async/await (not .then())
- Always include try/catch error handling
- Console.log your results to verify
You'll use these free APIs:
JSONPlaceholder (Fake REST API for testing)
- Base URL:
https://jsonplaceholder.typicode.com - Endpoints:
/users- 10 users/posts- 100 posts/comments- 500 comments/users/{id}- Single user/posts?userId={id}- Posts by user
Documentation: https://jsonplaceholder.typicode.com/guide/
Create an async function that:
- Fetches all users from
/users - Console.logs the array of users
- Returns the array
const fetchAllUsers = async () => {
// Your code here
};
fetchAllUsers();Expected output: Array of 10 user objects
Create an async function that:
- Fetches all users
- Creates an array of just the names (use .map())
- Console.logs the names
- Returns the names array
const getUserNames = async () => {
// Your code here
};
getUserNames();Expected output: ["Leanne Graham", "Ervin Howell", ...]
Create an async function that:
- Takes a userId parameter
- Fetches that specific user from
/users/{userId} - Console.logs the user object
- Returns the user
const getUser = async (userId) => {
// Your code here
};
getUser(1);
getUser(5);Expected output: Single user object
Create an async function that:
- Tries to fetch a user with an invalid ID (999)
- Uses try/catch to handle the error
- If successful, returns the user
- If error, console.logs "User not found" and returns null
const getUserSafely = async (userId) => {
// Your code here
};
getUserSafely(1); // Should work
getUserSafely(999); // Should handle gracefullyCreate an async function that:
- Fetches all posts from
/posts - Console.logs the total number of posts
- Returns the posts array
const getAllPosts = async () => {
// Your code here
};
getAllPosts();Expected output: 100 posts
Create an async function that:
- Takes a userId parameter
- Fetches all posts
- Filters to keep only posts by that userId (use .filter())
- Console.logs the filtered posts
- Returns the filtered array
const getPostsByUser = async (userId) => {
// Your code here
};
getPostsByUser(1);Expected output: Array of posts where userId === 1
Create an async function that:
- Takes a userId parameter
- Gets all posts by that user
- Extracts just the titles (use .map())
- Console.logs the titles
- Returns titles array
const getUserPostTitles = async (userId) => {
// Your code here
};
getUserPostTitles(1);Expected output: Array of post title strings
Create an async function that:
- Fetches all posts
- Creates an object showing how many posts each user has written
- Console.logs the result
- Returns the object
Hint: Loop through posts and count by userId
const countPostsByUser = async () => {
// Your code here
};
countPostsByUser();Expected output:
{
"1": 10,
"2": 10,
"3": 10,
// ...
}Create an async function that:
- Takes a userId parameter
- Fetches the user data from
/users/{userId} - Fetches that user's posts from
/posts?userId={userId} - Returns an object:
{ user, posts } - Console.logs the result
const getUserWithPosts = async (userId) => {
// Your code here
};
getUserWithPosts(1);Expected output:
{
user: { id: 1, name: "Leanne Graham", ... },
posts: [ { userId: 1, id: 1, title: "...", ... }, ... ]
}Create an async function that:
- Takes a userId parameter
- Gets user and their posts (reuse previous function or fetch both)
- Creates a summary object with:
- userName
- totalPosts (count)
- postTitles (array)
- Returns the summary
const getUserSummary = async (userId) => {
// Your code here
};
getUserSummary(1);Expected output:
{
userName: "Leanne Graham",
email: "Sincere@april.biz",
totalPosts: 10,
postTitles: ["sunt aut...", "qui est...", ...]
}Create an async function that:
- Fetches data for users 1, 2, and 3
- For each user, get their name and post count
- Returns array of objects:
{ name, postCount }
Hint: You'll need to make multiple async calls
const getMultipleUserStats = async () => {
// Your code here
};
getMultipleUserStats();Expected output:
[
{ name: "Leanne Graham", postCount: 10 },
{ name: "Ervin Howell", postCount: 10 },
{ name: "Clementine Bauch", postCount: 10 },
];Create an async function that:
- Fetches users and posts at the SAME TIME using Promise.all()
- Returns both:
{ users, posts } - Console.log how long it takes
Hint: Use console.time() and console.timeEnd()
const fetchUsersAndPosts = async () => {
console.time("Parallel fetch");
// Your code here
console.timeEnd("Parallel fetch");
};
fetchUsersAndPosts();Create an async function that:
- Fetches users, posts, AND comments all at once
- Uses Promise.all()
- Returns
{ users, posts, comments } - Console.logs the count of each
const fetchAllData = async () => {
// Your code here
};
fetchAllData();Expected output:
Users: 10
Posts: 100
Comments: 500
Create two functions:
fetchSequential()- fetches users, then posts, then comments (one after another)fetchParallel()- fetches all three at once
Time both and see the difference!
const fetchSequential = async () => {
console.time("Sequential");
// Fetch users
// THEN fetch posts
// THEN fetch comments
console.timeEnd("Sequential");
};
const fetchParallel = async () => {
console.time("Parallel");
// Fetch all at once with Promise.all()
console.timeEnd("Parallel");
};
fetchSequential();
fetchParallel();Create an async function that:
- Takes a postId parameter
- Fetches the post from
/posts/{postId} - Fetches comments for that post from
/comments?postId={postId} - Returns:
{ post, comments, commentCount }
const getPostWithComments = async (postId) => {
// Your code here
};
getPostWithComments(1);Create an async function that:
- Takes a search term parameter
- Fetches all posts
- Filters posts where the title includes the search term
- Returns matching posts
const searchPosts = async (searchTerm) => {
// Your code here
};
searchPosts("sunt");Create an async function that:
- Fetches all posts
- Counts posts per user
- Returns the top 3 users with most posts
- Format:
[{ userId, postCount }, ...]
const getTopPosters = async () => {
// Your code here
};
getTopPosters();Create an async function that:
- Takes a userId
- Fetches:
- User data
- User's posts
- Comments on user's posts (all of them)
- Returns complete profile:
{
user: { name, email, ... },
posts: [...],
totalComments: number,
averageCommentsPerPost: number
}const getCompleteUserProfile = async (userId) => {
// Your code here
};
getCompleteUserProfile(1);Create a function that generates blog statistics:
const getBlogStats = async () => {
// Fetch all necessary data
// Calculate and return:
return {
totalUsers: number,
totalPosts: number,
totalComments: number,
averagePostsPerUser: number,
averageCommentsPerPost: number,
mostActiveUser: { name, postCount },
mostCommentedPost: { title, commentCount },
};
};
getBlogStats().then((stats) => console.table(stats));Before submitting, make sure:
- All functions use async/await (not .then())
- All functions have try/catch error handling
- All functions console.log results
- Code is tested and working
- Variable names are clear
- Code is formatted nicely
Good luck! 🚀
Common Mistakes:
- Forgetting
awaitkeyword - Forgetting second
awaitfor.json() - Not handling errors with try/catch
- Using wrong endpoint URL
Debugging:
- Console.log the response before parsing
- Check Network tab in DevTools
- Verify the URL is correct
- Make sure fetch has both awaits
Testing:
// Always test your functions
const test = async () => {
const result = await yourFunction();
console.log("Result:", result);
};
test();