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 handle async tasks by allowing you to attach callbacks for success or failure.
Click to reveal answer
beginner
How do you create a new Promise?
You create a Promise by using the
new Promise() constructor and passing a function with two parameters: resolve and reject. Inside this function, you perform your async task and call resolve() if it succeeds or reject() if it fails.Click to reveal answer
beginner
What do the
resolve and reject functions do in a Promise?resolve is called when the async operation finishes successfully, passing the result. reject is called when the operation fails, passing an error or reason.Click to reveal answer
beginner
Show a simple example of creating a Promise that resolves after 1 second.
Example:<br><pre>const waitOneSecond = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Done waiting!');
}, 1000);
});</pre>Click to reveal answer
beginner
Why use Promises instead of callbacks?
Promises make async code easier to read and manage by avoiding "callback hell". They provide a clear way to handle success and errors, and can be chained for sequential async tasks.
Click to reveal answer
What are the two functions passed to the Promise constructor?
✗ Incorrect
The Promise constructor receives a function with two parameters: resolve (for success) and reject (for failure).
Which method is used to handle a successful Promise result?
✗ Incorrect
The then() method is used to handle the result when a Promise resolves successfully.
What does calling reject() inside a Promise do?
✗ Incorrect
Calling reject() marks the Promise as failed or rejected, triggering error handling.
How do you create a Promise that waits 2 seconds before resolving?
✗ Incorrect
You pass a function to the Promise constructor that calls resolve after 2000 milliseconds using setTimeout.
Why are Promises preferred over nested callbacks?
✗ Incorrect
Promises help avoid deeply nested callbacks, making asynchronous code cleaner and easier to follow.
Explain how to create a Promise and what the roles of resolve and reject are.
Think about how you tell the Promise when the task is done or failed.
You got /4 concepts.
Describe a simple example where a Promise waits 1 second before completing.
Imagine waiting for a timer before saying 'done'.
You got /3 concepts.