Promises Flashcards
What is an asynchronous operation?
An asynchronous operation is one that allows the program to move on to other tasks while waiting for the asynchronous operation to complete or resolve.
Name two situations where we might want to utilize asynchronous operations?
- Requesting information or querying a database
- Requesting information from a network or server
What are JavaScript promises?
JavaScript promises are objects that represent the eventual outcome of an asynchronous operation.
What are the three states of a JavaScript promise?
- Pending - the initial state
- Rejected - the operation has failed
- Fulfilled - the operation has succeeded
If a JavaScript promise has failed, what is returned?
An error is returned that we can access
If a JavaScript promise is fulfilled what is returned?
A resolved value is returned. This resolved value can taken many forms and get be a JSON object or other data form.
If a promise is not pending, what is it?
A promise would be considered settled, regardless of the outcome, if it is no longer pending.
How would we create a promise?
We would use the new Promise constructor. The promise takes one parameter, the executorFunction.
What role does the executorFunction play in defining a new promise.
The executorFunction determines what happens when a promise settles as either resolved or rejected.
How many parameters does an executorFunction take?
Two, it takes a resolved parameter and a rejected parameter.
What do we mean when we say we are consuming a promise?
We mean that we are using an already created promise and using the information returned upon either resolution or rejection.
How do we consume a promise?
We use the .then( ) method on a promise to consume the returned data.
How do we indicate what to do with the data returned from a settled promise?
The .then( ) method is a higher-order function that takes two callback functions to handle and manipulate either the resolved data or the rejected data.
Describe the syntax involved with a .then( ) method
functionCallingPromise(parameter).then(handleSuccess, handleFailure)
How can we pass the handler functions to the .then( ) method?
We can pass handler functions as either:
- function definitions (i.e. const success = function(res){ })
- anonymous functions (.then(function(success){ }, function(err){ }).
How many handler functions will .then( ) accept?
It will accept either 0, 1, or 2 functions, but it is best to past 2 for more readable code.
What does the .then( ) method return?
The .then( ) method returns a promise
Can you chain .then( ) methods?
Yes, since .then( ) returns a promise, you can then further utilize the returned values in another .then( ) method with succes and failure handlers.
How can we use the .catch( ) method to make our code cleaner and separate concerns?
The .catch( ) method will accept one callback function, the errorHandler.
We can pass our .then( ) the successHandler function and chain the .catch( ) method onto the end to handle failures.