Promises Flashcards

1
Q

What does a promise represent?

A

A promise represents the completion of an asynchronous function.

It is an object representing the eventual completion or failure of an asynchronous operation.

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

Prior to promises, how did we get around the problems that single-thread presents?

A

Usually events and callbacks.

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

What are the possible result states of a promise?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How many arguments does a then() take?

A

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.

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

Where can I find more info on promises?

A

At the following url for one:

https://developers.google.com/web/fundamentals/primers/promises

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

What is the syntax for creating a promise?

A

new Promise((resolve, reject) -> {})

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

When is a promise pending?

A

Until you give the promise something to resolve/reject, it will sit there as pending with a value of undefined.

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

When is a promise settled?

A

Once a promise is resolved/rejected, it is settled.

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

What are callbacks?

A

Callbacks are functions that have been passed as arguments to other functions.

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

What is promise chaining?

A

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.

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

What is the most intuitive way of handling promises?

A

Using async/await can help you write code that’s more intuitive and resembles synchronous code.

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

In a promise chain, what does each promise represent?

A

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

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

Can you have a then after a catch in a promise chain?

A

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.

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

What happens if a promise rejection event is not handled by any handler?

A

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.

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

What are the composition tools for running async operations together?

A

There are four composition tools for running asynchronous operations concurrently:

Promise.all()
Promise.allSettled()
Promise.any()
Promise.race().

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

What is a Promise.all()?

A

The Promise.all() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input’s promises fulfill (including when an empty iterable is passed), with an array of the fulfillment values. It rejects when any of the input’s promises rejects, with this first rejection reason.

17
Q

What is a Promise.allSettled()?

A

The Promise.allSettled() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input’s promises settle (including when an empty iterable is passed), with an array of objects that describe the outcome of each promise.

18
Q

What is a Promise.any()?

A

The Promise.any() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when any of the input’s promises fulfills, with this first fulfillment value. It rejects when all of the input’s promises reject (including when an empty iterable is passed), with an AggregateError containing an array of rejection reasons.

19
Q

What is a Promise.race()?

A

The Promise.race() static method takes an iterable of promises as input and returns a single Promise. This returned promise settles with the eventual state of the first promise that settles.

20
Q

What is an example of an old-style API that still expects success and/or failure callbacks to be passed?

A

The most obvious example is the setTimeout() function:

setTimeout(() => saySomething(“10 seconds passed”), 10 * 1000);

21
Q

If a callback to SetTimeout fails, can the error be caught?

A

Luckily we can wrap setTimeout in a promise. The best practice is to wrap the callback-accepting functions at the lowest possible level, and then never call them directly again:

const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

wait(10 * 1000)
.then(() => saySomething(“10 seconds”))
.catch(failureCallback);

22
Q

What is another name for callback hell?

A

Pyramid of doom.

23
Q

What is the superpower of promise chains?

A

Each function will only be called when the previous promise has resolved, and it’ll be called with that promise’s output.

24
Q

What things can you do in a then statement?

A
  1. return another promise
  2. return a synchronous value (or undefined)
  3. throw a synchronous error
25
Q

What should you always do in a then function?

A

You should always return or throw from inside a then() function.

26
Q

How can the normal new Promise declaration containing just a resolve be expressed?

A

Instead of :

new Promise(function (resolve, reject) {
resolve(someSynchronousValue);
}).then(/* … */);

you can just say:

Promise.resolve(someSynchronousValue).then(/* … */);

27
Q

Which is preferable, to use the 2nd argument to then or to use a catch?

A

The preference should be to use a catch as it will catch all errors whereas the 2nd argument to a then, may lead to an error not being reported.

Note, that this may not be the case when writing async tests in Mocha.