Promises help manage asynchronous operations by avoiding callback hell and making code easier to read and maintain.
Click to reveal answer
beginner
How do promises improve code readability?
Promises allow chaining with .then() and .catch(), which makes asynchronous code look more like synchronous code.
Click to reveal answer
beginner
What are the three states of a promise?
A promise can be pending (waiting), fulfilled (success), or rejected (error).
Click to reveal answer
beginner
Why is callback hell a problem?
Callback hell happens when callbacks are nested deeply, making code hard to read, debug, and maintain.
Click to reveal answer
beginner
How do promises handle errors better than callbacks?
Promises use .catch() to handle errors in one place, avoiding scattered error handling in nested callbacks.
Click to reveal answer
What does a promise represent in JavaScript?
AA value that may be available now, later, or never
BA synchronous function
CA type of variable
DA way to declare constants
✗ Incorrect
A promise represents a value that might not be ready yet but will be resolved in the future.
Which method is used to handle a successful promise result?
A.resolve()
B.catch()
C.finally()
D.then()
✗ Incorrect
.then() is used to handle the successful completion of a promise.
What state is a promise in before it is fulfilled or rejected?
AResolved
BPending
CRejected
DCompleted
✗ Incorrect
A promise starts in the pending state before it is either fulfilled or rejected.
How do promises help avoid callback hell?
ABy chaining asynchronous operations cleanly
BBy nesting callbacks deeper
CBy ignoring errors
DBy using synchronous code
✗ Incorrect
Promises allow chaining with .then(), making asynchronous code easier to read and avoiding deeply nested callbacks.
Which method is used to catch errors in promises?
A.then()
B.finally()
C.catch()
D.error()
✗ Incorrect
.catch() is used to handle errors in promise chains.
Explain why promises are used in JavaScript and how they improve asynchronous code.
Think about how promises make waiting for data easier and cleaner.
You got /4 concepts.
Describe the three states of a promise and what each means.
Consider what happens from start to finish of a promise.
You got /3 concepts.
Practice
(1/5)
1. Why are promises used in JavaScript? Promise helps with:
easy
A. Storing data permanently
B. Making the program run slower
C. Writing code without any functions
D. Handling tasks that take time without stopping the program
Solution
Step 1: Understand what promises do
Promises allow JavaScript to handle tasks like loading data without freezing the program.
Step 2: Identify the correct purpose
Promises help manage asynchronous tasks so the program keeps running smoothly.
Final Answer:
Handling tasks that take time without stopping the program -> Option D
Quick Check:
Promises = handle async tasks without blocking [OK]
Hint: Promises handle slow tasks without freezing code [OK]
Common Mistakes:
Thinking promises slow down the program
Confusing promises with data storage
Believing promises remove the need for functions
2. Which of the following is the correct way to create a promise in JavaScript?
easy
A. let p = new Promise((resolve, reject) => { /* code */ });
B. let p = Promise.create(() => { /* code */ });
C. let p = promise(() => { /* code */ });
D. let p = new promise { /* code */ };
Solution
Step 1: Recall correct Promise syntax
Promises are created with new Promise and a function with resolve and reject parameters.
Step 2: Check each option
Only let p = new Promise((resolve, reject) => { /* code */ }); uses the correct syntax with new Promise and arrow function.
Final Answer:
let p = new Promise((resolve, reject) => { /* code */ }); -> Option A
Quick Check:
Correct Promise syntax = new Promise(...) [OK]
Hint: Use 'new Promise' with resolve and reject functions [OK]
Common Mistakes:
Using lowercase 'promise' instead of 'Promise'
Trying to call Promise.create which doesn't exist
Missing parentheses or arrow function syntax
3. What will be the output of this code?
const p = new Promise((resolve) => {
setTimeout(() => resolve('Done'), 100);
});
p.then(result => console.log(result));
console.log('Start');
medium
A. Start Done
B. Done Start
C. Done Done
D. Start Start
Solution
Step 1: Understand asynchronous behavior
The setTimeout delays the resolve call by 100ms, so console.log('Start') runs first.
Step 2: Trace the output order
First, 'Start' prints immediately, then after 100ms, 'Done' prints from the promise.
Final Answer:
Start Done -> Option A
Quick Check:
Async delay means 'Start' before 'Done' [OK]
Hint: setTimeout delays promise resolution, so logs appear after immediate code [OK]
Common Mistakes:
Assuming promise resolves immediately
Thinking 'Done' prints before 'Start'
Ignoring asynchronous nature of setTimeout
4. Find the error in this promise code:
const p = new Promise((resolve, reject) => {
resolve('Success');
reject('Error');
});
p.then(result => console.log(result))
.catch(error => console.log(error));
medium
A. Promise must call reject first
B. Calling reject after resolve has no effect
C. Missing semicolon after resolve
D. Then and catch cannot be chained
Solution
Step 1: Understand promise state changes
Once a promise is resolved or rejected, its state is final and cannot change.
Step 2: Analyze the code flow
The promise calls resolve('Success') first, so the later reject('Error') is ignored.
Final Answer:
Calling reject after resolve has no effect -> Option B
Quick Check:
Promise settles once; later calls ignored [OK]
Hint: Promise settles once; later resolve/reject calls ignored [OK]
Common Mistakes:
Thinking reject overrides resolve if called later
Believing then and catch can't be chained
Confusing semicolon placement with logic errors
5. You want to fetch user data and then fetch posts for that user. Which approach best uses promises to handle this sequence?
hard
A. Use setTimeout to delay fetchPosts() after fetchUser()
B. Call fetchUser() and fetchPosts() separately without waiting
C. Call fetchUser(), then inside its .then(), call fetchPosts() and return that promise
D. Call fetchPosts() first, then fetchUser() inside its .then()
Solution
Step 1: Understand promise chaining for dependent tasks
When one task depends on another, use .then() to wait for the first before starting the second.
Step 2: Identify correct chaining
Call fetchUser(), then inside its .then(), call fetchPosts() and return that promise correctly calls fetchUser(), then in its .then(), calls fetchPosts() and returns its promise to chain properly.
Final Answer:
Call fetchUser(), then inside its .then(), call fetchPosts() and return that promise -> Option C
Quick Check:
Promise chaining = dependent tasks in sequence [OK]
Hint: Chain promises with then() for dependent async tasks [OK]
Common Mistakes:
Running dependent tasks in parallel without waiting
Using setTimeout instead of promise chaining
Calling tasks in wrong order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.