asynchronous programming Flashcards

1
Q

callback function

A

a function passes as an argument to another function which is called after a certain task has been completed

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

callback hell/ pyramid of doom

A

multiple nested callbacks making the code hard to read an maintain

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

what can we use to avoid callback hell

A

promises

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

what is a promise and how does it work

A

a js object that links a producing and consuming code
processing is deferred to a later point in time whilst we do something else and we react once the promise has been completed

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

what are the three states and their results of promises

A

pending, undefined; initial state (waiting for something to happen)
fulfilled, value; operation successful
rejected, error; operation failed

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

how do nested promises work

A

when a promise is returned inside a callback of another promise
the outer one waits for the inner one to resolve/reject before continuing

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

how do we create promises

A

promise takes a function with two parameters; resolve and reject and the value of either will be returned once the promise finishes

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

how do we use (consume) a promise

A

we defines .then() (runs when the promise resolves) and .catch() (runs when the promise rejects)

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

what does parallel execution of promises do and what are the three things we can call to do this

A

manages promises that can run independently of each other
using promise.all(), promise.race() and promise.any()

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

promise.all()

A

takes an array of promise and returns a new promise that resolves when all promises in the array do
or rejects with the reason of the first rejected promise

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

promise.race()

A

takes an array of promises and returns a new promise that resolves/rejects as soon as one of them does not waiting for the rest

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

promise.any()

A

resolves when any of the promises resolves
if they all reject then so will any

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

what is the key difference between promise.any() and promise.race()

A

race matches the first promise whilst any must resolve

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

how does async and wait work

A

an async function implicitly returns a promise and you use await to pause the function execution until it resolves

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