Promises Flashcards

To understand the concepts & syntax behind JS Promises

1
Q

What are the 3 states of the Promise object

A

Fulfilled, Rejected and Pending

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can a Promise be in more than 1 state at a time?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Picture an example of the Promise Constructor

A
var promise1 = new Promise(function(resolve, reject) {
  setTimeout(resolve, 100, 'foo');
});
console.log(promise1);
// expected output: [object Promise]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the name of the function passed to the Promise constructor?

A

The executor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What arguments are passed to the executor

A

Resolve, Reject

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are 2 ways to handle exceptions in a Promise?

A
  1. ) By using the .catch() method

2. ) By passing a second function to the .then() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name 3 benefits of Promises

A
  1. ) Improved error handling
  2. ) Logical syntax
  3. ) Chaining
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens if we return a value from the .catch() method?

A

We can chain after a catch. The value is passed back and can be handled with further .then / .catch chaining.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens when an exception is thrown but there are 5 .then() methods chained after it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Promise.all() for?

A

For resolving an array of Promises once each one has completed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens if 1 promise fails in the Promise.all([]) array?

A

We exit the execution and hit the .catch error handler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When is .then() call synchronously?

A

Never - functions passed to .then() will never be called synchronously, even with an already-resolved promise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which errors does a nested .catch() statement handle?

A

Only failures within its scope and below, not higher up in the chain outside its scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the only 3 things you can do inside a .then() function

A
  1. ) Return another promise
  2. ) Return a synchronous value, or undefined
  3. ) Throw a synchronous error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are 3 things to ask yourself when using a promise?

A
  1. ) Are you returning another promise?
  2. ) Are you returning a value?
  3. ) Are you returning an error?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens inside the .catch() method if an exception is never thrown?

A

Nothing, the .catch() method is skipped entirely.

17
Q

How are synchronous return values treated in the chain?

A

A synchronous value is automatically converted into a resolved Promise.

A synchronous error is automatically converted into a rejected Promise.