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
Recall & Review
beginner
What is a Promise in JavaScript?
A Promise is an object that represents the eventual completion or failure of an asynchronous operation. It helps write cleaner code by avoiding deeply nested callbacks.
Click to reveal answer
beginner
What are the three states of a Promise?
A Promise can be in one of three states: pending (initial state), fulfilled (operation completed successfully), or rejected (operation failed).
Click to reveal answer
beginner
How does the then() method work with Promises?
The then() method is used to specify what to do when a Promise is fulfilled. It takes a callback function that runs with the resolved value.
Click to reveal answer
beginner
What is the purpose of the catch() method in Promises?
The catch() method handles errors or rejections from a Promise. It runs a callback if the Promise is rejected, helping to manage failures cleanly.
Click to reveal answer
intermediate
How do Promises help make asynchronous code cleaner compared to callbacks?
Promises avoid "callback hell" by chaining then() calls instead of nesting callbacks. This makes the code easier to read and maintain.
Click to reveal answer
Which Promise state means the operation is still running?
ARejected
BPending
CFulfilled
DResolved
✗ Incorrect
A Promise is 'pending' while the asynchronous operation is still in progress.
Which method is used to handle a successful Promise result?
Athen()
Bcatch()
Cfinally()
Dresolve()
✗ Incorrect
The then() method runs a callback when the Promise is fulfilled successfully.
What does the catch() method do in a Promise chain?
AReturns the Promise value
BStarts the Promise
CHandles errors or rejections
DCancels the Promise
✗ Incorrect
catch() handles errors or rejected Promises to keep code clean and avoid crashes.
Which of these is NOT a Promise state?
AResolved
BRejected
CFulfilled
DPending
✗ Incorrect
'Resolved' is not an official Promise state; the correct states are pending, fulfilled, and rejected.
Why are Promises preferred over nested callbacks?
AThey run faster
BThey avoid using functions
CThey use less memory
DThey make code easier to read and maintain
✗ Incorrect
Promises allow chaining which avoids deeply nested callbacks, making code cleaner and easier to follow.
Explain how Promises improve asynchronous code compared to callbacks.
Think about how you handle success and errors without nesting functions inside each other.
You got /5 concepts.
Describe the lifecycle of a Promise from creation to completion.
Imagine waiting for a package delivery: waiting, delivered, or failed.
You got /5 concepts.
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]