0
0
Javascriptprogramming~5 mins

Creating promises in Javascript - Quick Revision & Summary

Choose your learning style9 modes available
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) =&gt; {
  setTimeout(() =&gt; {
    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?
Athen and catch
Bstart and stop
Cresolve and reject
Dopen and close
Which method is used to handle a successful Promise result?
Acatch()
Bthen()
Cfinally()
Dstart()
What does calling reject() inside a Promise do?
APauses the Promise
BMarks the Promise as fulfilled
CRestarts the Promise
DMarks the Promise as rejected
How do you create a Promise that waits 2 seconds before resolving?
Anew Promise(resolve => setTimeout(resolve, 2000))
Bnew Promise(reject => setTimeout(reject, 2000))
CPromise.wait(2000)
DPromise.delay(2000)
Why are Promises preferred over nested callbacks?
AThey make code easier to read and manage
BThey use less memory
CThey run faster
DThey don't require functions
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.