Promises Flashcards Preview

Phase_2 > Promises > Flashcards

Flashcards in Promises Deck (10)
Loading flashcards...
1

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

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.

2

Use .then to handle when a JS Promise is fulfilled

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

3

Use .catch to handle when a JS Promise is rejected

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 );

4

Use .then to chain JS Promises.

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 );

5

Create a new Promise in JS

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

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

6

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

Rly simple.

7

Describe and use Promise.all( ) in JS

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"]
});

8

Describe and use Promise.race( ) in JS

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.

9

Describe and use Promise.reject( ) in JS

Promise.reject( iterable );

10

Describe and use Promise.resolve( ) in JS

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