Challenge - 5 Problems
Service Layer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What does this Express service layer return?
Consider this service function called by a route handler. What will be the resolved value when calling
getUserById(2)?Express
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
function getUserById(id) {
return new Promise((resolve) => {
const user = users.find(u => u.id === id);
resolve(user ? user.name : null);
});
}Attempts:
2 left
💡 Hint
Look at what the promise resolves with: the user's name or the whole user object.
✗ Incorrect
The service function finds the user object by id and resolves the promise with the user's name property if found, otherwise null. For id 2, the user is {id: 2, name: 'Bob'}, so it resolves with 'Bob'.
❓ state_output
intermediate1:30remaining
What is the final state of
req.user after middleware and service call?Given this Express middleware and service call, what will
req.user contain after next()?Express
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
function userService(id) {
return users.find(u => u.id === id) || null;
}
function authMiddleware(req, res, next) {
req.user = userService(1);
next();
}Attempts:
2 left
💡 Hint
Check what userService returns for id 1.
✗ Incorrect
The middleware calls userService with id 1, which returns the user object {id: 1, name: 'Alice'}. This is assigned to req.user before calling next().
📝 Syntax
advanced2:00remaining
Which option correctly implements an async service function in Express?
You want to create a service function that fetches user data asynchronously. Which option is syntactically correct and returns a promise resolving to the user object?
Express
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];Attempts:
2 left
💡 Hint
Remember that Array.find is synchronous and does not return a promise.
✗ Incorrect
Option B correctly returns a Promise that resolves with the user object. Option B is async but returns a synchronous value, which is allowed but may confuse. Option B uses await outside async function causing syntax error. Option B awaits a synchronous function, which is allowed but unnecessary.
🔧 Debug
advanced2:00remaining
Why does this Express service call cause an unhandled promise rejection?
Examine the code below. Why does calling
getUserById(3) cause an unhandled promise rejection?Express
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
function getUserById(id) {
return new Promise((resolve, reject) => {
const user = users.find(u => u.id === id);
if (!user) reject('User not found');
else resolve(user);
});
}
getUserById(3).then(user => console.log(user));Attempts:
2 left
💡 Hint
Check how the promise rejection is handled after calling getUserById.
✗ Incorrect
The promise rejects with 'User not found' when no user is found. The caller uses then() but no catch() to handle rejection, causing unhandled promise rejection error.
🧠 Conceptual
expert1:30remaining
What is the main benefit of using a service layer in Express apps?
Why do developers use a service layer between Express route handlers and data access logic?
Attempts:
2 left
💡 Hint
Think about code organization and responsibilities.
✗ Incorrect
The service layer separates business logic from routing, improving maintainability and testability. It does not automatically cache or replace middleware or bypass abstraction.