W4.1 Asynch & State - callbacks and promises Flashcards

1
Q

Does JS have threads?

A

No

But runtime environment does have threads

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

What are the 2 styles of asynchronous code in JS?

A
  • Callbacks

- Promises

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

What is a callback function?

A

A function passed by reference as a parameter - not executed immediately. It is “called back” asynchronously in the containing function’s body (usually in response to an event)

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

Describe the use of callbacks in a JS runtime environment

A
  • call a function that takes a callback function and it will run asynchronously
  • when the callback completes (i.e event happens) the completed task is put on a queue
  • when stack is empty another thread in runtime environment (event loop) takes tasks off the queue and pushes the tasks onto the stack where they are executed (See diagram)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the event loop in a runtime environment?

A

a semi-infinite loop, polling (waiting) and blocking on the OS until something is ready - exits when there are no more events to wait on

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

Give examples of things that are pollable (i.e held in event loop) and things that are not (implemented by thread pool).

A
Pollable:
- TCP/UDP sockets - http/https
- stdin, stdout, stderr
- timeouts
- asynch DNS operations
- signals
Not pollable: 
- filesystems
- DNS lookup
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a promise in JS?

A
  • A promise is an object representing the eventual completion or failure of an asynch operation
  • a promise is a returned object that callbacks are attached to (instead of passing them into a function)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What three fields does a promise have?

A
  • object that is promised
  • function used to resolve/fulfil promise
  • function used to reject promise
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What three states can a promise be in?

A
  • fulfilled
  • rejected
  • pending
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What syntax is used for assigning fulfil/resolve and reject functions for a promise?

A

p.then(f, r)
where f is onFulfilled and r is onRejected.

Once promise fulfilled/rejected the respective handler function is called asynchronously.

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

How does promise chaining work?

A
  • when one is fulfilled move onto the next (fulfilled promise object is returned by handler and used as the next promise’s value)
  • each then leads to more asynchronous actions and promises
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an asynchronous function?

A
  • uses async function declaration (AsyncFunction object)

- function which executes asynchronously via the event loop, using implicit promise to return its result

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