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

Speaker 1Speaker 2

Async JavaScript Pitfalls

4:25
0:00-4:25
  1. Maria

    So Simon, I just listened to this talk on async and streaming JavaScript, and it really got me thinking about how messy our async code can get.

  2. Simon

    Oh absolutely. The speaker starts off by acknowledging that we're probably doing it wrong, and I love that honesty. But tell me, what's the main problem they highlight?

  3. Maria

    Well, it's that every library does async differently – callbacks, promises, events – each covering its own little piece but not the whole story. And when you try to combine them, like making a web request and then waiting for a key press without overloading the server, it becomes really hard.

  4. Simon

    Right. And that's where callback hell comes in. The speaker shows that nested callbacks for something like playing a movie – initialize, authorize, get ticket – result in code that looks like a cow's head, totally losing the logical flow.

  5. Maria

    Exactly. And event-based programming isn't much better. They gave an example of creating a custom drag event where you have to manage state for mousedown, mousemove, mouseup, and add and remove listeners manually. It's really easy to mess up.

  6. Simon

    So that's the problem. But then they introduce a way to think about the landscape – two axes: synchronous vs asynchronous, and single value vs multiple values. That gives us four quadrants.

  7. Maria

    Yes! For synchronous single values we just call a function, for synchronous multiple values we have iterables like arrays with map, filter, and forEach. For asynchronous single values we have callbacks and promises. And for asynchronous multiple values – that's where observables come in.

  8. Simon

    And the key insight is that these are all first-class objects you can pass around. You can hand someone a promise and say 'this represents a future value'. You can't do that with a callback.

  9. Maria

    Exactly. Promises are great for unwinding callback hell – they let you chain operations linearly. In the movie example, you go from player initializing to authorizing to playing, with proper error handling. Much cleaner.

  10. Simon

    But promises have limitations too. The speaker points out that cancellation isn't part of the ES6 Promise contract. So if you have an autocomplete request that becomes irrelevant, you can't easily cancel it. You'd need a cancellation token or use a library like Bluebird.

  11. Maria

    Right. And that's where observables shine. The speaker's philosophy is that everything is a stream – your mouse is a database of coordinates. You can compose streams using operators like map, filter, flatMap, takeUntil, just like you do with arrays.

  12. Simon

    They showed that mouse drag example again, but this time with Rx. You take the mousedown stream, flatMap it into mousemove, compute offsets, and then takeUntil mouseup. No manual state management – it's all declarative.

  13. Maria

    And for autocomplete, they showed throttle (or debounce) so you only fire when the user stops typing. Then you can even handle out-of-order responses by using something like `flatMapLatest` to only take the latest response.

  14. Simon

    So the core idea is that observables unify the asynchronous multiple values case. And they're first-class, so you can pass them around and compose them. The speaker also clarifies that this isn't FRP – FRP deals with continuous time behaviors, whereas observables are discrete events.

  15. Maria

    Right. They also touched on generators and async/await. Generators with yield let you write async code in a linear style, but they still require a runner like co. Async/await simplifies that further, and it's been approved for ES7.

  16. Simon

    And then streams – Node streams have evolved from the messy Streams1 to Streams2 and Streams3 with object mode, cork/uncork for buffering. But the speaker feels it's still too imperative. WhatWG streams aim to standardize lower-level I/O in browsers.

  17. Maria

    The reactive streams initiative addresses back pressure in observable systems. In a multicast scenario, you might want to control the flow. Observables can handle back pressure with lossy methods like sampling or lossless methods like pauseable buffers or request-n.

  18. Simon

    And for the future, the speaker mentions async generators, a proposal by Jafar Husain, that would let you write the mouse drag example with a for-await-of loop. That would be really intuitive.

  19. Maria

    So the big takeaway is that we need better abstractions for async. Observables give us a powerful, composable way to handle event streams, and they're already making their way into languages like Dart. It's an exciting space.

  20. Simon

    Absolutely. And the speaker's final message was that asynchronous programming is hard, but with the right tools, we can conquer it. It's not about burritos or monads – it's about thinking in streams.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent