Promises Flashcards
To understand the concepts & syntax behind JS Promises
What are the 3 states of the Promise object
Fulfilled, Rejected and Pending
Can a Promise be in more than 1 state at a time?
No
Picture an example of the Promise Constructor
var promise1 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, 'foo'); });
console.log(promise1); // expected output: [object Promise]
What is the name of the function passed to the Promise constructor?
The executor
What arguments are passed to the executor
Resolve, Reject
What are 2 ways to handle exceptions in a Promise?
- ) By using the .catch() method
2. ) By passing a second function to the .then() method
Name 3 benefits of Promises
- ) Improved error handling
- ) Logical syntax
- ) Chaining
What happens if we return a value from the .catch() method?
We can chain after a catch. The value is passed back and can be handled with further .then / .catch chaining.
What happens when an exception is thrown but there are 5 .then() methods chained after it?
It doesn’t matter, the exception falls to the next .catch() method. A promise chain stops if there’s an exception.
* Keeping in mind that if you decide to continue execution you can by returning a value.
What is Promise.all() for?
For resolving an array of Promises once each one has completed
What happens if 1 promise fails in the Promise.all([]) array?
We exit the execution and hit the .catch error handler.
When is .then() call synchronously?
Never - functions passed to .then() will never be called synchronously, even with an already-resolved promise.
Which errors does a nested .catch() statement handle?
Only failures within its scope and below, not higher up in the chain outside its scope.
What are the only 3 things you can do inside a .then() function
- ) Return another promise
- ) Return a synchronous value, or undefined
- ) Throw a synchronous error
What are 3 things to ask yourself when using a promise?
- ) Are you returning another promise?
- ) Are you returning a value?
- ) Are you returning an error?