Check out a free preview of the full Rethinking Asynchronous JavaScript course

The "Exercise 8" Lesson is part of the full, Rethinking Asynchronous JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

In this exercise, you will create your own streams and stream responses.


Transcript from the "Exercise 8" Lesson

>> [MUSIC]

>> Kyle Simpson: Exercise eight is designed to get you a little bit of practice playing around with the notion of streams, and making your own stream responses. Let me quickly orient you exercise eight.
>> Kyle Simpson: You'll notice that exercise eight starts out with a button and a list, and you're gonna subscribe to the click event we're using jQuery here just to make things simple.

So you don't have to worry about DOMstuff. We're gonna subscribe to the click event on that button, and then we're gonna do something. And it's that something that you're gonna have to figure out how to do. But elsewhere in our app, that is down on line nine, we're gonna listen to a sequence that's gonna be a sampled sequence.

So this is what I mean. Sampling is a time based thing. So let's say you click the button really, really fast. I only wanna hear a single event every 500 milliseconds. I wanna throttle that stream, so I'm gonna have a stream that's coming fast to send in a bunch of data down.

But I wanna create a composed stream that only sends me out an event ever 500 milliseconds. And ignores and drops all those repetitive ones that are coming too fast. And I wanna subscribe to that sampled stream, and drop those messages into the list box. So just to show you visually what you're gonna be expecting.

Let me pop up the fixed version of exercise eight, just so you visually understand what you're trying to go for here. So if I click the click me, it adds a single message to it. Click it again, it adds another message. But if I start clicking it really fast, you'll notice that I'm only getting one event every 500 milliseconds.

Okay, that's it, that's all we're doing. You play around with how you can set yourself up with a set interval, and sample a stream and see what you come up with. So we'll take about, let's call this 15 minutes for you to play around with this. It's not a lot of code.

If you need some hints, look at the fixed version. But this should get your brain starting to think about events as our pattern for async.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now