Promises Flashcards

1
Q

What is an asynchronous operation?

A

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.

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

Name two situations where we might want to utilize asynchronous operations?

A
  1. Requesting information or querying a database
  2. Requesting information from a network or server
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are JavaScript promises?

A

JavaScript promises are objects that represent the eventual outcome of an asynchronous operation.

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

What are the three states of a JavaScript promise?

A
  1. Pending - the initial state
  2. Rejected - the operation has failed
  3. Fulfilled - the operation has succeeded
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If a JavaScript promise has failed, what is returned?

A

An error is returned that we can access

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

If a JavaScript promise is fulfilled what is returned?

A

A resolved value is returned. This resolved value can taken many forms and get be a JSON object or other data form.

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

If a promise is not pending, what is it?

A

A promise would be considered settled, regardless of the outcome, if it is no longer pending.

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

How would we create a promise?

A

We would use the new Promise constructor. The promise takes one parameter, the executorFunction.

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

What role does the executorFunction play in defining a new promise.

A

The executorFunction determines what happens when a promise settles as either resolved or rejected.

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

How many parameters does an executorFunction take?

A

Two, it takes a resolved parameter and a rejected parameter.

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

What do we mean when we say we are consuming a promise?

A

We mean that we are using an already created promise and using the information returned upon either resolution or rejection.

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

How do we consume a promise?

A

We use the .then( ) method on a promise to consume the returned data.

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

How do we indicate what to do with the data returned from a settled promise?

A

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.

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

Describe the syntax involved with a .then( ) method

A

functionCallingPromise(parameter).then(handleSuccess, handleFailure)

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

How can we pass the handler functions to the .then( ) method?

A

We can pass handler functions as either:

  1. function definitions (i.e. const success = function(res){ })
  2. anonymous functions (.then(function(success){ }, function(err){ }).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How many handler functions will .then( ) accept?

A

It will accept either 0, 1, or 2 functions, but it is best to past 2 for more readable code.

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

What does the .then( ) method return?

A

The .then( ) method returns a promise

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

Can you chain .then( ) methods?

A

Yes, since .then( ) returns a promise, you can then further utilize the returned values in another .then( ) method with succes and failure handlers.

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

How can we use the .catch( ) method to make our code cleaner and separate concerns?

A

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.

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

How does using .then( ) and .catch( ) separate our concerns?

A

By isolating our successHandler function and rejectHandler function into two methods we can make our code easier to read.

21
Q

What are two reasons we chain .then( ) methods onto a promise?

A

We chain .then( ) statements when:

  1. we need to use the returned data from the original promise in other ways, perhaps another database request, or a different API call.
  2. we need operations to occur in a specific order
22
Q

What is the process of chaining promises together called?

A

It is called composition.

23
Q

When we chain .then( ) methods, or promises, what must we keep in mind when utilizing returned values?

A

We must keep in mind that we should explicitly return the result of the manipulation of the previous promise value or we will simply get the former returned value again.

24
Q

Can we use anonymous functions as handler functions

A

Yes, we can use anonymous functions as handler functions for both .then( ) and .catch( )

25
Q

What are two mistakes made when chaining promises?

A
  1. Nesting statements instead of chaining them
  2. Forgetting to return a promise
26
Q

What happens if we forget to return a promise from

chained .then( ) methods?

A

The chained methods will return the original promise resolved value

27
Q

What do we mean by concurrency in regards to JavaScript promises?

A

We mean multiple asynchronous operations running at the same time.

28
Q

What function can we use to use concurrency in JavaScript?

A

We can use the Promise.all( ) funciton.

29
Q

How is the Promise.all( ) function used in JavaScript to run multiple asynchronous operations at once?

A

We pass an array of promises to the Promise.all( ) function.

  1. If every promise in the passed array is resolved, Promise.all returns an array of the resolved values of all passed promises.
  2. If any promise in the passed array rejects, the single promise returned from Promise.all will reject with the reason for rejection.
30
Q

const myPromise = new Promise((res,rej))

If we ran typeof myPromise what would be the result?

A

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

31
Q

How many parameters does a Promise constructor take?

A

The Promise constructor takes one parameter - the executor function

32
Q

What are async …. await considered in regards to JavaScript promises?

A

async … await are considered syntactic sugar in regards to JavaScript promises.

They do not introduce new functionality, but make the syntax easier to write and read

33
Q

What is the await keyword considered to be?

A

It is considered to be an operator and can only be used within async functions.

34
Q

What does the await operator do for us?

A

It returns the resolved value of a promise, halting or pausing the code block until the promise is resolved.

35
Q

How do we typically use the async and await keywords?

A

Typically we declare an async function:

async function myFunction() {

then we set a variable equal to the returned promise value

let/const promiseValue = await myPromise( );

then we do something with the returned value

36
Q

What happens if we forget the await keyword when using async functions?

A

If we forget the await keyword the function will not be paused and we will get a Promise: pending as a result (or object: promise).

This can be difficult to debug as the function will still run, but the output will be different (and if they are chained will trickle down).

37
Q

What is one drawback when using .catch in a long promise chain?

A

In a long promise chain it can be difficult to deduce where the error was thrown when using .catch.

38
Q

How do we use catch in async … await blocks in our code?

A

We can catch errors with .catch in our async … await code by using the syntax try { } catch { }

39
Q

What is different when using the catch method in an async … await operation setting as opposed to a .then() setting?

A

In a try catch block we do not use the dot operator ( . ) to call try or catch. They are used much as an if … else statement

40
Q

Does the catch of a try … catch block catch synchronous errors as well as asynchronous errors?

A

Yes, catch will catch both asynchronous and synchronous errors in our code.

41
Q

Do we need to pass the catch method any parameters in a try .. catch block?

A

No. If we do not need to use the exception identifier then we do not need to pass any parameters. If, however, we want to log the error out, or do something else with it, we should pass a parameter representing the error.

42
Q

What is an exception identifier?

A

An exception identifier is an identifier that holds the exception value in a try … catch block (it is the parameter that we pass to the catch method, the e or err that holds the error value)

43
Q

How do we use the resolved value of an async … await function?

A

We typically save the resolved value to a variable:

let result = await myAsyncFunction( );

use result somehow.

44
Q

What can we do if we want our promise to run concurrently but need all values to resolve before taking the next step in our code block?

A

We can assign the promise values to variables, without the await keyword. These promises will settle as they are able.

Then we can await the result of doing the code action that utilizes all resolved promises. We still get the pause in code resolution, but the promises do not wait for one another to attempt resolution.

45
Q

Can we use Promise.all to run concurrent promises in JavaScript?

A

Yes, we can pass multiple promises, in an array, to Promise.all(). We prepend the keyword await to the Promise.all() to wait for the promises to all resolve before being able to access the resolves values as indices of the returned array.

46
Q

What do we pass to Promise.all()?

A

We pass an array of promises to Promise.all()

47
Q

What do we use the await keyword for in JavaScript?

A

We use the await keyword to pause execution of our function until an asynchronous action completes and the awaited promise is no longer pending

48
Q

What should we do if we have a string of promises that do not rely on or utilize the value of the preceding promise?

A

We should use concurrency to allow all promises to run in parallel.

49
Q

When do we use the finally( ) method?

A

The finally( ) method allows us to execute a code block regardless of how a promise settles. It follows the .then( ) and .catch( ) chain.