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

  • 19Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Async Challenges and Promises

What is 'callback hell' in JavaScript?

19 questions

  1. 1

    What is 'callback hell' in JavaScript?

    Model answer

    Callback hell refers to deeply nested callback functions that make code difficult to read and maintain, often resulting in a 'cow's head' shape where logic is lost.

    Explanation

    Nested callbacks create a pyramid of doom that obscures the sequential flow, making debugging and maintenance hard.

  2. 2

    What are two common issues with event-based programming for implementing a drag gesture?

    Model answer

    You must manage state (whether mouse is down, start position) and manually add and remove event listeners for mousedown, mousemove, and mouseup.

    Explanation

    State management across multiple events and explicit listener registration complicate event-driven code.

  3. 3

    Why is a promise considered a first-class object?

    Model answer

    You can pass a promise to a function and return it from a function, representing an eventual value.

    Explanation

    First-class status means promises can be manipulated like any other value, enabling composability.

  4. 4

    What is a major limitation of the ES6 Promise contract?

    Model answer

    It does not include cancellation, which is important for use cases like auto-complete where you may no longer care about a request.

    Explanation

    Cancellation is not part of the spec; developers have to implement workarounds like cancellation tokens.

  5. 5

    In the asynchronous programming landscape, what does the synchronous/single-value quadrant represent?

    Model answer

    A simple value obtained by calling a function or assigning a variable.

    Explanation

    This quadrant covers immediate, non-async data retrieval.

  6. 6

    What does the synchronous/multiple-value quadrant represent?

    Model answer

    An iterable collection such as arrays, maps, sets, or any object implementing the iterable protocol.

    Explanation

    Synchronous collections can be iterated over immediately.

  7. 7

    What does the asynchronous/single-value quadrant contain?

    Model answer

    Callbacks and promises.

    Explanation

    These mechanisms handle a single future value asynchronously.

  8. 8

    What does the asynchronous/multiple-value quadrant contain?

    Model answer

    Observables.

    Explanation

    Observables represent streams of asynchronous values over time.

  9. 9

    How does the speaker define reactive programming?

    Model answer

    Reactive programming means responding to events, load, failure, and most importantly, the user, keeping the application responsive.

    Explanation

    Beyond event handling, reactive systems adapt to changing conditions.

  10. 10

    What is an observable in the context of asynchronous programming?

    Model answer

    An observable is a first-class object that represents a potentially infinite stream of asynchronous values, allowing operations like map, filter, and forEach.

    Explanation

    Observables enable declarative manipulation of async data streams similar to collections.

  11. 11

    How would you implement a mouse drag using observables?

    Model answer

    Use flatMap on the mousedown observable to combine with mousemove, map to calculate offsets, and takeUntil the mouseup observable to stop the stream.

    Explanation

    flatMap flattens inner observables, map transforms values, and takeUntil terminates the stream on mouseup.

  12. 12

    How can you prevent overloading a server in an auto-complete with observables?

    Model answer

    Use throttle (or debounce) to only fire an event after the user stops typing for a specified time, e.g., half a second.

    Explanation

    Throttling reduces the rate of requests, avoiding unnecessary server calls.

  13. 13

    What is the purpose of the yield keyword in generators?

    Model answer

    It suspends the function's execution and resumes when a value is obtained, enabling asynchronous control flow.

    Explanation

    Yield allows pausing and resuming generator functions, which can be used with promise libraries for async code.

  14. 14

    What is async/await and when was it approved for ES?

    Model answer

    Async/await is a syntax for asynchronous programming that makes it explicit that a function is asynchronous; it was approved for the first stage of ES7.

    Explanation

    Async/await provides a more linear, imperative style over promises.

  15. 15

    What is an async generator as proposed by Jafar Husain?

    Model answer

    An async generator would allow using a for/on loop over event streams, e.g., for (let move of mousedown.flatMap(...)) to yield events.

    Explanation

    Async generators combine generators and async iteration for event streams.

  16. 16

    Compare Node.js streams version 1 and version 2.

    Model answer

    Streams1 had issues like pause not working and data being pushed immediately; streams2 (0.9/0.10) added object mode, proper pause/resume, and readable/writable classes.

    Explanation

    Streams2 improved control over data flow, but the speaker still finds them too imperative.

  17. 17

    What is the purpose of whatWG streams?

    Model answer

    They aim to standardize lower-level I/O APIs for browsers, with concepts like cast (one reader, one writer).

    Explanation

    whatWG streams provide a common interface for streaming data in browsers.

  18. 18

    How does Dart handle asynchronous I/O?

    Model answer

    Dart unifies I/O and events using futures and streams, allowing the same API for both.

    Explanation

    Dart's approach merges async data sources into a consistent programming model.

  19. 19

    What are the three categories of back pressure in reactive streams?

    Model answer

    Lossy (sampling, throttling), lossless (pausable buffer, pause/resume), and controlled (request N items per subscription).

    Explanation

    Back pressure mechanisms allow consumers to control the rate of data flow.

1 of 19
Question 1 of 19

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent