Promises Flashcards

1
Q

Describe the difference between a JS Promise being fulfilled and it being rejected.

A

A Promise that is fulfilled means it successfully executed and returned the desired result, a rejected promise means it failed. Every promise function can use a resolve and reject method to accomplish either of the above.

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

Use .then to handle when a JS Promise is fulfilled

A

Promise is a method, a global method available to Node. This means you never have to require it into your app.

Promise itself takes one argument: an anonymous function. That anonymous function takes resolve and reject arguments. Note: inside our anon function we handle the reject( ) and the resolve( ) methods!

Our Promise function can have a then( ) appended to it.
Our Promise function can also have a catch( ) appended.

You can also choose to call Promise.resolve( ) or Promise.reject( ) and append either one with your then( ) and/or catch( ).

This is one way to use the Promise( ). We could also choose to write a function that returns a new Promise( ).

https://www.youtube.com/watch?v=s6SH72uAn3Q

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

Use .catch to handle when a JS Promise is rejected

A
Promise.resolve( )
.then(Function1).catch(errorHandler1)
.then(Function2).catch(errorHandler2)
.then(Function3).catch(errorHandler3)
.then(Function4).catch(errorHandler4)
.catch( finalErrorHandler ); 

// insulated catches below
Promise.resolve( )
.then(function() { return Function1().catch(errorHandler1); })
.then(function() { return Function2().catch(errorHandler2); })
.then(function() { return Function3().catch(errorHandler3); })
.then(function() { return Function4().catch(errorHandler4); })
.catch( finalErrorHandler );

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

Use .then to chain JS Promises.

A
Promise.resolve( )
.then(Function1).catch(errorHandler1)
.then(Function2).catch(errorHandler2)
.then(Function3).catch(errorHandler3)
.then(Function4).catch(errorHandler4)
.catch( finalErrorHandler ); 

// insulated catches below
Promise.resolve( )
.then( function( ) { return Function1().catch(errorHandler1); })
.then( function( ) { return Function2().catch(errorHandler2); })
.then( function( ) { return Function3().catch(errorHandler3); })
.then( function( ) { return Function4().catch(errorHandler4); })
.catch( finalErrorHandler );

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

Create a new Promise in JS

A
var p1 = new Promise( (resolve, reject) => {
  resolve('Success!');
  // or
  // reject ("Error!");
} );

p1.then( value => {
console.log(value); // Success!
}, reason => {
console.log(reason); // Error!
} );

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

Convert a function that uses callbacks into function that returns a JS promise

A

Rly simple.

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

Describe and use Promise.all( ) in JS

A

Promise.all( iterable );

An iterable is an object such as an array or a string. Think of this as a map function, we pass into our all( ) an array of objects and then we append a then( ) method to the end of the all( ) and do something with the iterator values.

var p1 = Promise.resolve(3);
var p2 = 1337;
var p3 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'foo');
}); 

Promise.all([p1, p2, p3]).then(values => {
console.log(values); // [3, 1337, “foo”]
});

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

Describe and use Promise.race( ) in JS

A

Promise.race( iterable );

The race method on the Promise object resolves or rejects a promise as soon as one of the promises in the iterable completes.

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

Describe and use Promise.reject( ) in JS

A

Promise.reject( iterable );

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

Describe and use Promise.resolve( ) in JS

A

Promise.resolve( value );
Promise.resolve( promise );
Promise.resolve( thenable );

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