Promises Flashcards
What does a promise represent?
A promise represents the completion of an asynchronous function.
It is an object representing the eventual completion or failure of an asynchronous operation.
Prior to promises, how did we get around the problems that single-thread presents?
Usually events and callbacks.
What are the possible result states of a promise?
A promise can be:
fulfilled - The action relating to the promise succeeded
rejected - The action relating to the promise failed
pending - Hasn’t fulfilled or rejected yet
settled - Has fulfilled or rejected
How many arguments does a then() take?
then() takes two arguments, a callback for a success and another for a failure case. Both are optional, so you can add a callback for the success or failure case only.
Where can I find more info on promises?
At the following url for one:
https://developers.google.com/web/fundamentals/primers/promises
What is the syntax for creating a promise?
new Promise((resolve, reject) -> {})
When is a promise pending?
Until you give the promise something to resolve/reject, it will sit there as pending with a value of undefined.
When is a promise settled?
Once a promise is resolved/rejected, it is settled.
What are callbacks?
Callbacks are functions that have been passed as arguments to other functions.
What is promise chaining?
A common need is to execute two or more asynchronous operations back to back, where each subsequent operation starts when the previous operation succeeds, with the result from the previous step.
With promises, we accomplish this by creating a promise chain. The API design of promises makes this great, because callbacks are attached to the returned promise object, instead of being passed into a function.
What is the most intuitive way of handling promises?
Using async/await can help you write code that’s more intuitive and resembles synchronous code.
In a promise chain, what does each promise represent?
In a promise chain, each promise represents the completion of one asynchronous step in the chain. In addition, the arguments to then are optional, and catch(failureCallback) is short for then(null, failureCallback) — so if your error handling code is the same for all steps, you can attach it to the end of the chain (as one catch-all so to speak).
Can you have a then after a catch in a promise chain?
Yes, it’s possible to chain after a failure, i.e. a catch, which is useful to accomplish new actions even after an action failed in the chain.
What happens if a promise rejection event is not handled by any handler?
If a promise rejection event is not handled by any handler, it bubbles to the top of the call stack, and the host needs to surface it.
What are the composition tools for running async operations together?
There are four composition tools for running asynchronous operations concurrently:
Promise.all()
Promise.allSettled()
Promise.any()
Promise.race().