Promises and Fetch Flashcards

1
Q

Can you pause execution of code in JavaScript?

A

No, there is no idea. You can’t sleep the thread

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

What do promises try to solve?

A

The situation where you are waiting for data to come back from the internet or another source

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

What are the three states of promises?

A

Unresolved, resolved, rejected

1) Unresolved - waiting for something to finish
2) Resolved - something finished and it’s all okay
3) Rejected - something finished and something went bad

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

After the unresolved status, what happens when you enter resolved or rejected?

A

Then or Catch (Respectfully)

You also utilize the then or catch with callbacks

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

When do you use promises?

A

When you do AJAX requests. However, you can make an AJAX request without a promise and a promise without an AJAX request

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

Explain how catch operates

A

When a promise hits the resolved state and the callback function is ran, you can then call ‘variable name’.then() with a callback inside to run some additional code.

promise = new Promise((resolve, reject) =>{

reject();

});

promise

.then(() => console.log(“Finally Finished!”))

.then(()=> console.log(“I was also ran!”))

.catch(()=> console.log(‘Uh on!’))

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

What is the most popular handler to use with promises?

A

The Fetch handler is the most popular in ES6

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

What are the shortcomings of fetch?

A

You need to call .json() for your first then, then you can call data to get the data that you need

BIG: If you are using fetch and it has .then and . catch. IF the server returms back an error, it does NOT enter the .catch statement which is different from EVERY library. Only enters if the request flat out fails.

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