Asynchrnouous Javascript Flashcards

(16 cards)

1
Q

A _______ is a function that you
write and then pass to some other function

A

callback

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

One of the simplest kinds of asynchrony is when you want to run some
code after a certain amount of time has elapsed. As we saw in §11.10,
you can do this with the ___________ function

A

setTimeout()

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

The first argument to setTimeout() is a ______ and the second is
a _____ interval measured in milliseconds

A

function, time

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

A ______ is an object that represents the result of an asynchronous
computation

A

Promise

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

In other words, actions that we initiate now, but they finish later.

A

asynchronous actions

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

callback

A

When your done, call this

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

Why are callbacks necessary

A

Because in javascript operations that take a long time such as reading a file, network requets etc, the program could continue on even if that process has not finished exectuing, and if we try to access that later, could result in an error

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

The “producing code” takes whatever time it needs to produce the promised result, and the “______” makes that result available to all of the subscribed code when it’s ready.

A

promise

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

Constructor Syntax for promise object

A

let promise = new Promise(function(resolve, reject) {
// executor (the producing code, “singer”)
});

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

The function passed to new Promise is called the ______

A

executor

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

When new Promise is created, the ______ runs automatically. It contains the producing code which should eventually produce the result.

A

executor

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

Its arguments _____ and _____ are callbacks provided by JavaScript itself. Our code is only inside the executor.

A

resolve, reject

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

let promise = new Promise(function(resolve, reject) {
// the function is executed automatically when the promise is constructed

// after 1 second signal that the job is done with the result “done”
setTimeout(() => resolve(“done”), 1000);
});

A

The executor is called automatically and immediately (by new Promise).

The executor receives two arguments: resolve and reject. These functions are pre-defined by the JavaScript engine, so we don’t need to create them. We should only call one of them when ready.

After one second of “processing”, the executor calls resolve(“done”) to produce the result. This changes the state of the promise object:

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

then syntax

A

promise.then(
function(result) { /* handle a successful result / },
function(error) { /
handle an error */ }
);

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

If we’re interested only in errors, then we can use ____ as the first argument: .then(null, errorHandlingFunction). Or we can use .catch(errorHandlingFunction), which is exactly the same:

A

null

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

The executor should call only ____ resolve or one reject. Any state change is final.

All further calls of resolve and reject are ______