Async JavaScript Pitfalls
- 24Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is callback hell primarily characterized by?
What is callback hell primarily characterized by?
24 questions
- 1
What is callback hell primarily characterized by?
- A
Nested functions that lose linear logic
- B
Synchronous blocking operations
- C
Too many event listeners
- D
Excessive use of promises
Correct answer
ANested functions that lose linear logic
Explanation
Callback hell refers to deeply nested callback functions that obscure the flow of logic, often described as a 'cow's head' shape in the talk.
- A
- 2
In the drag event example, what state variable is tracked?
- A
The number of times the mouse has moved
- B
The current time of each click
- C
Whether the mouse is down or not
- D
The distance from the edge of the screen
Correct answer
CWhether the mouse is down or not
Explanation
The talk mentions needing a state to track if the mouse is down, along with the start position, to implement a custom drag event.
- A
- 3
What problem arises when adding new functionality to stateful event-based code?
- A
State juggling becomes unmanageable
- B
The code becomes synchronous
- C
Events no longer fire
- D
Callbacks are automatically flattened
Correct answer
AState juggling becomes unmanageable
Explanation
The speaker notes that when dealing with stateful event-based code, adding new features leads to juggling state that becomes unmanageable.
- A
- 4
In the four quadrants of asynchronous programming, which quadrant does an observable belong to?
- A
Asynchronous multiple values
- B
Synchronous single value
- C
Asynchronous single value
- D
Synchronous multiple values
Correct answer
AAsynchronous multiple values
Explanation
Observables are placed in the fourth quadrant: asynchronous and multiple values, allowing operations like map/filter/forEach on async streams.
- A
- 5
In the four quadrants model, what represents a synchronous single value?
- A
A promise or callback
- B
A function call or variable assignment
- C
An iterable such as an array
- D
An observable sequence
Correct answer
BA function call or variable assignment
Explanation
Synchronous single value is described as calling a function or assigning a value directly.
- A
- 6
What is a key advantage of promises over callbacks in terms of first-class citizenship?
- A
Promises are objects that can be passed around and returned
- B
Promises do not require any library
- C
Promises eliminate all errors
- D
Promises are always synchronous
Correct answer
APromises are objects that can be passed around and returned
Explanation
The talk emphasizes that promises are first-class objects representing a future value, which can be passed to functions and returned, unlike callbacks.
- A
- 7
What advantage do observables have over iterables?
- A
Observables are asynchronous and can represent streams over time
- B
Observables cannot be filtered
- C
Observables are always finite
- D
Observables can only be consumed once
Correct answer
AObservables are asynchronous and can represent streams over time
Explanation
Observables handle asynchronous sequences (e.g., mouse moves) and can be queried like collections, whereas iterables are synchronous.
- A
- 8
What is a limitation of ES6 promises mentioned in the talk?
- A
They cannot handle multiple values
- B
They require a polyfill for all environments
- C
Cancellation is not part of the contract
- D
They are not supported in modern browsers
Correct answer
CCancellation is not part of the contract
Explanation
The speaker notes that cancellation is not included in the ES6 promise specification, leaving developers to find their own solutions.
- A
- 9
How do promises help avoid callback hell in the movie example?
- A
By removing the need for error handling
- B
By automatically retrying failed operations
- C
By chaining .then() calls to linearize the flow
- D
By making all operations synchronous
Correct answer
CBy chaining .then() calls to linearize the flow
Explanation
Promises allow chaining with .then() to sequence operations like player initialization and authorization without nesting, producing a linear flow.
- A
- 10
What is NOT a suggested approach for promise cancellation mentioned in the talk?
- A
Passing a cancellation token
- B
Using a library like 'last' from Domenic Denicola
- C
Creating a cancellation promise that subclasses Promise
- D
Ignoring the cancellation requirement
Correct answer
DIgnoring the cancellation requirement
Explanation
The talk lists subclassing Promise, using a cancellation token, or libraries as options; ignoring cancellation is not a valid approach.
- A
- 11
In an autocomplete scenario, what operator prevents server overload?
- A
TakeUntil
- B
Throttle or debounce
- C
FlatMap
- D
Merge
Correct answer
BThrottle or debounce
Explanation
The talk mentions throttle/debounce to delay firing until after a pause to avoid excessive requests.
- A
- 12
What does flatMap do with observables?
- A
Flattens a sequence of sequences into a single stream
- B
Filters out unwanted values
- C
Combines two observables into one
- D
Maps each value to a promise
Correct answer
AFlattens a sequence of sequences into a single stream
Explanation
FlatMap takes a sequence of sequences (like nested observables) and flattens them into one stream of values.
- A
- 13
In the mouse drag observable solution, what operator stops the sequence on mouse up?
- A
TakeWhile
- B
Filter
- C
TakeUntil
- D
Skip
Correct answer
CTakeUntil
Explanation
The talk uses takeUntil(mouseUp) to complete the drag sequence when the mouse button is released.
- A
- 14
How does Rx handle out-of-order responses from multiple requests?
- A
By queuing responses in order of arrival
- B
By canceling all previous requests
- C
By rejecting out-of-order responses
- D
By using operators that only keep the latest response
Correct answer
DBy using operators that only keep the latest response
Explanation
Rx can use operators like switchLatest (or flatMapLatest) to ensure only the last response is used, avoiding out-of-order issues.
- A
- 15
What does the
yieldkeyword do in a generator?- A
Throws an error if called outside a generator
- B
Returns a value and terminates the generator
- C
Suspends the generator function and resumes when a value is requested
- D
Creates a new promise
Correct answer
CSuspends the generator function and resumes when a value is requested
Explanation
The talk explains that yield suspends the function and resumes when the next value is obtained via the generator's next() method.
- A
- 16
What is a benefit of async/await over generators?
- A
Allows multiple yields in one function
- B
Works without a transpiler
- C
Eliminates all callbacks
- D
Makes asynchronous code look synchronous and explicit
Correct answer
DMakes asynchronous code look synchronous and explicit
Explanation
Async/await provides a clearer, more linear syntax for asynchronous code compared to generators, which require manual yield management.
- A
- 17
What stage of approval did async/await reach for ES7 according to the talk?
- A
Stage 2 (draft)
- B
Stage 4 (finished)
- C
Stage 3 (candidate)
- D
Stage 1 (proposal)
Correct answer
DStage 1 (proposal)
Explanation
The talk states that async/await was approved for ES7 at least at the first stage, which is Stage 1 (proposal).
- A
- 18
What was a major problem with Node streams v1?
- A
They were only available in Node 0.8
- B
Pause and resume were impossible, and data was pushed all at once
- C
They required a callback for every chunk
- D
They did not support object mode
Correct answer
BPause and resume were impossible, and data was pushed all at once
Explanation
The talk describes streams v1 as awful because pause didn't work, data didn't start properly, and you couldn't consume a specific number of bytes.
- A
- 19
What feature did Node streams v2 introduce?
- A
Cork and uncork
- B
Back pressure from the start
- C
Object mode and instant flow control
- D
Automatic error handling
Correct answer
CObject mode and instant flow control
Explanation
Streams v2 (landing in 0.9) introduced object mode and instant now (flow control), along with readable/writable classes.
- A
- 20
What is the purpose of cork/uncork in streams v3?
- A
To buffer writes and flush them in bulk
- B
To pause and resume the stream
- C
To transform data chunk by chunk
- D
To create a duplex stream
Correct answer
ATo buffer writes and flush them in bulk
Explanation
The talk mentions cork/uncork as useful for dealing with mass data writes by buffering writes until uncork is called.
- A
- 21
What is the goal of WHATWG streams?
- A
To create a new protocol for streaming
- B
To add streams to the DOM
- C
To replace Node streams entirely
- D
To standardize lower-level streaming APIs for browsers
Correct answer
DTo standardize lower-level streaming APIs for browsers
Explanation
The talk says WHATWG streams aim to standardize lower-level APIs for browsers, with work from Dominik.
- A
- 22
What is a lossless back pressure strategy for observables?
- A
Dropping values
- B
Pauseable buffer (pause and resume)
- C
Throttling
- D
Sampling
Correct answer
BPauseable buffer (pause and resume)
Explanation
The talk lists pauseable buffer as a lossless strategy: pausing and resuming ensures all events between are still received.
- A
- 23
In reactive streams, how can a consumer control data flow?
- A
By converting the stream to a promise
- B
By stopping the observable entirely
- C
By using a queue to fetch data manually
- D
By requesting a specific number of items per subscription
Correct answer
DBy requesting a specific number of items per subscription
Explanation
The talk mentions that each subscription can request 'N' number of items as a way to implement back pressure.
- A
- 24
What challenge does multi-cast back pressure address?
- A
Handling multiple subscribers with different consumption rates
- B
Ensuring all subscribers get the same data
- C
Preventing duplicate events
- D
Reducing memory usage
Correct answer
AHandling multiple subscribers with different consumption rates
Explanation
Multi-cast back pressure is needed when multiple subscribers consume data at different speeds, unlike single-cast scenarios like Node streams.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.