Promise Flashcards

1
Q

A Promise

A

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

Guarantees that a promise comes with

A
  • Callbacks will never be called before the completion of the current run of the JavaScript event loop
  • Callbacks added with then(), even after a promise has been resolved or rejected, will still be called
  • Multiple callbacks may be added by calling then() several times.
    • each callback is executed one after another, in the order they were inserted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

createAudioFileAsync(audioSettings).then(successCallback, failureCallback)

is shorthand for what?

A

const promise = createAudioFileAsync(audioSettings)

promise.then(successCallback, failureCallback)

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

Promise Chain

A
  • Ability to execute two or more asynchronous tasks back-to-back
  • Each subsequent operation starts when the previous succeeds, with the result from the previous step
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why must you always return results?

A

So that other callbacks will receive the results of a previous promise.

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

What is chaining after a catch?

A

When you place a then() after a catch() to accomplish some actions even after there was a failure.

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

What is error propogation?

A
  • When a promise chain stops if there’s an exception.
  • It looks down the chain for catch handlers instead
    • modeling try/catch in synchronous code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the only native asychronous functions in JavaScript?

A

setTimeout and setInterval

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

What are three forms of asynchronous execution?

A
  • Http requests
  • Any I/O operation
  • Dealing with a websocket
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When does any function passed to setTimeout execute?

A
  • Executes asynchronously
  • No less than the delay amount, but possibly more
  • When the main thread is not busy anymore
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is asynchronous code needed?

A

The main thread can’t execute the code and refresh the user interface at the same time.

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

What does the then function do?

A

It binds one or two callbacks to the promise, and can use the data given to the resolve or reject function.

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

How many times can you call resolve or reject in a promise?

A

Just one. Calling resolve or reject more than once is useless as only the first one will be recognized.

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

When should you use Promise.resolve()?

A

When you’re in a conditional and could either return a promise or a simple value. Use Promise.resolve(simpleValue) so that a promise is always being returned.

function test() {

` if (something) {`

` return aNewPromise()`

` } else {`

` return Promise.resolve(42)`

` }`

}

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

How do you handle logging and recover from the error in a promise?

A

Simply use a catch block to recover and send errors to logging, and then rethrow the error at the end of the catch block using throw err.

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

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