1. Home
  2. Explore
  3. Computer Science
  4. Async JavaScript Pitfalls

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Async JavaScript Pitfalls

  • 24Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Callback Hell and State Management

What is callback hell primarily characterized by?

24 questions

  1. 1

    What is callback hell primarily characterized by?

    1. A

      Nested functions that lose linear logic

    2. B

      Synchronous blocking operations

    3. C

      Too many event listeners

    4. D

      Excessive use of promises

    Correct answer
    A

    Nested 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.

  2. 2

    In the drag event example, what state variable is tracked?

    1. A

      The number of times the mouse has moved

    2. B

      The current time of each click

    3. C

      Whether the mouse is down or not

    4. D

      The distance from the edge of the screen

    Correct answer
    C

    Whether 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.

  3. 3

    What problem arises when adding new functionality to stateful event-based code?

    1. A

      State juggling becomes unmanageable

    2. B

      The code becomes synchronous

    3. C

      Events no longer fire

    4. D

      Callbacks are automatically flattened

    Correct answer
    A

    State 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.

  4. 4

    In the four quadrants of asynchronous programming, which quadrant does an observable belong to?

    1. A

      Asynchronous multiple values

    2. B

      Synchronous single value

    3. C

      Asynchronous single value

    4. D

      Synchronous multiple values

    Correct answer
    A

    Asynchronous multiple values

    Explanation

    Observables are placed in the fourth quadrant: asynchronous and multiple values, allowing operations like map/filter/forEach on async streams.

  5. 5

    In the four quadrants model, what represents a synchronous single value?

    1. A

      A promise or callback

    2. B

      A function call or variable assignment

    3. C

      An iterable such as an array

    4. D

      An observable sequence

    Correct answer
    B

    A function call or variable assignment

    Explanation

    Synchronous single value is described as calling a function or assigning a value directly.

  6. 6

    What is a key advantage of promises over callbacks in terms of first-class citizenship?

    1. A

      Promises are objects that can be passed around and returned

    2. B

      Promises do not require any library

    3. C

      Promises eliminate all errors

    4. D

      Promises are always synchronous

    Correct answer
    A

    Promises 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.

  7. 7

    What advantage do observables have over iterables?

    1. A

      Observables are asynchronous and can represent streams over time

    2. B

      Observables cannot be filtered

    3. C

      Observables are always finite

    4. D

      Observables can only be consumed once

    Correct answer
    A

    Observables 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.

  8. 8

    What is a limitation of ES6 promises mentioned in the talk?

    1. A

      They cannot handle multiple values

    2. B

      They require a polyfill for all environments

    3. C

      Cancellation is not part of the contract

    4. D

      They are not supported in modern browsers

    Correct answer
    C

    Cancellation 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.

  9. 9

    How do promises help avoid callback hell in the movie example?

    1. A

      By removing the need for error handling

    2. B

      By automatically retrying failed operations

    3. C

      By chaining .then() calls to linearize the flow

    4. D

      By making all operations synchronous

    Correct answer
    C

    By 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.

  10. 10

    What is NOT a suggested approach for promise cancellation mentioned in the talk?

    1. A

      Passing a cancellation token

    2. B

      Using a library like 'last' from Domenic Denicola

    3. C

      Creating a cancellation promise that subclasses Promise

    4. D

      Ignoring the cancellation requirement

    Correct answer
    D

    Ignoring the cancellation requirement

    Explanation

    The talk lists subclassing Promise, using a cancellation token, or libraries as options; ignoring cancellation is not a valid approach.

  11. 11

    In an autocomplete scenario, what operator prevents server overload?

    1. A

      TakeUntil

    2. B

      Throttle or debounce

    3. C

      FlatMap

    4. D

      Merge

    Correct answer
    B

    Throttle or debounce

    Explanation

    The talk mentions throttle/debounce to delay firing until after a pause to avoid excessive requests.

  12. 12

    What does flatMap do with observables?

    1. A

      Flattens a sequence of sequences into a single stream

    2. B

      Filters out unwanted values

    3. C

      Combines two observables into one

    4. D

      Maps each value to a promise

    Correct answer
    A

    Flattens 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.

  13. 13

    In the mouse drag observable solution, what operator stops the sequence on mouse up?

    1. A

      TakeWhile

    2. B

      Filter

    3. C

      TakeUntil

    4. D

      Skip

    Correct answer
    C

    TakeUntil

    Explanation

    The talk uses takeUntil(mouseUp) to complete the drag sequence when the mouse button is released.

  14. 14

    How does Rx handle out-of-order responses from multiple requests?

    1. A

      By queuing responses in order of arrival

    2. B

      By canceling all previous requests

    3. C

      By rejecting out-of-order responses

    4. D

      By using operators that only keep the latest response

    Correct answer
    D

    By 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.

  15. 15

    What does the yield keyword do in a generator?

    1. A

      Throws an error if called outside a generator

    2. B

      Returns a value and terminates the generator

    3. C

      Suspends the generator function and resumes when a value is requested

    4. D

      Creates a new promise

    Correct answer
    C

    Suspends 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.

  16. 16

    What is a benefit of async/await over generators?

    1. A

      Allows multiple yields in one function

    2. B

      Works without a transpiler

    3. C

      Eliminates all callbacks

    4. D

      Makes asynchronous code look synchronous and explicit

    Correct answer
    D

    Makes 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.

  17. 17

    What stage of approval did async/await reach for ES7 according to the talk?

    1. A

      Stage 2 (draft)

    2. B

      Stage 4 (finished)

    3. C

      Stage 3 (candidate)

    4. D

      Stage 1 (proposal)

    Correct answer
    D

    Stage 1 (proposal)

    Explanation

    The talk states that async/await was approved for ES7 at least at the first stage, which is Stage 1 (proposal).

  18. 18

    What was a major problem with Node streams v1?

    1. A

      They were only available in Node 0.8

    2. B

      Pause and resume were impossible, and data was pushed all at once

    3. C

      They required a callback for every chunk

    4. D

      They did not support object mode

    Correct answer
    B

    Pause 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.

  19. 19

    What feature did Node streams v2 introduce?

    1. A

      Cork and uncork

    2. B

      Back pressure from the start

    3. C

      Object mode and instant flow control

    4. D

      Automatic error handling

    Correct answer
    C

    Object 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.

  20. 20

    What is the purpose of cork/uncork in streams v3?

    1. A

      To buffer writes and flush them in bulk

    2. B

      To pause and resume the stream

    3. C

      To transform data chunk by chunk

    4. D

      To create a duplex stream

    Correct answer
    A

    To 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.

  21. 21

    What is the goal of WHATWG streams?

    1. A

      To create a new protocol for streaming

    2. B

      To add streams to the DOM

    3. C

      To replace Node streams entirely

    4. D

      To standardize lower-level streaming APIs for browsers

    Correct answer
    D

    To 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.

  22. 22

    What is a lossless back pressure strategy for observables?

    1. A

      Dropping values

    2. B

      Pauseable buffer (pause and resume)

    3. C

      Throttling

    4. D

      Sampling

    Correct answer
    B

    Pauseable buffer (pause and resume)

    Explanation

    The talk lists pauseable buffer as a lossless strategy: pausing and resuming ensures all events between are still received.

  23. 23

    In reactive streams, how can a consumer control data flow?

    1. A

      By converting the stream to a promise

    2. B

      By stopping the observable entirely

    3. C

      By using a queue to fetch data manually

    4. D

      By requesting a specific number of items per subscription

    Correct answer
    D

    By 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.

  24. 24

    What challenge does multi-cast back pressure address?

    1. A

      Handling multiple subscribers with different consumption rates

    2. B

      Ensuring all subscribers get the same data

    3. C

      Preventing duplicate events

    4. D

      Reducing memory usage

    Correct answer
    A

    Handling 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.

Question 1 of 24
Question 1 of 24

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent