Drop and Delay Observable Emissions with RxJS debounce

André Staltz
InstructorAndré Staltz
Share this video with your friends

Social Share Links

Send Tweet

you drop some emissions, while at the same time delaying the other emissions in the case emissions Debounce and debounceTime operators are similar to delayWhen and delay, with the difference that they may drop some emissions. This lesson teaches you how debounce works and where is it useful, for instance when building a type-ahead UI.

🚨 Since we are importing interval from RxJS, we don't need to preface our Observables with Rx.Observable. You can no longer .{operator}, you need to .pipe({operator}) instead. To link together multiple Observables and Operators, the method is a bit different. You call zip first, then list your Observables, your functions, then pipe your Operators.

[00:00] The next operators we're going to learn are debounce and debounceTime, which are quite similar to delayWhen and Delay. While Delay time shifts each emission by one second, debounce waits for one second of silence, and then it emits the most recent value.

[00:19] I know those two sentences sounded quite similar, but there's a big difference. DebounceTime also takes the same parameter here, but we're going to highlight one of its properties, which is it waits for that much of silence.

[00:34] What does silence actually mean? It's these dashes here. This input observable emits every 100 milliseconds, so it means that this much here is 100 milliseconds.

[00:46] As you can see, these dashes here represent 100 milliseconds of silence, but they're not enough for being 1,000 milliseconds of silence. Let's look at how debounceTime works as a marble diagram. It takes the input, which is zero, the input emission, and then it keeps that in memory. It has a memory inside it. Then it starts seeking for 1,000 milliseconds of event silence, which would be this much.

[01:17] As you can see, we didn't have enough silence, so it just drops zero in its memory, and it keeps one in its memory. Then it keeps on doing that for one, as well. It tries to look for one second of event silence, but it didn't have enough of event silence. It just keeps on doing that.

[01:37] Then finally, here with four, nothing happened after four and complete happened. No event happened here. There was no emission of any value, and that's exactly when it will emit four and complete here, because it had enough silence. It had one second of silence.

[01:59] If we run this, and let's just call debounceTime first, when we run it, we see just four and done. As you can see, debounceTime was dropping all of these values, because they didn't have enough silence. If you would change the parameter to say something like 50, which is much shorter than 100, so it's probably this much of time.

[02:26] It means that here, in this case, it does have enough event silence for each of these emissions, so debounceTime with 50, we're going to see all of these values. It didn't drop any of those. It actually behaved, in this case, just like Delay.

[02:46] DebounceTime has this property that it's able to drop things if they happen too fast. That's why we want to use debounceTime. In case things are happening too fast, we don't want all of those events that happened in that sequence of fast emissions. We want only the most recent one, which is four, in that case. DebounceTime is quite useful for cases like building a type ahead, for instance.

[03:16] Here, we have a case where, whenever I change something on this input field, it will immediately change the label here at the bottom. With debounceTime, you're able to say, "Only show or update the label after one second has passed that the user hasn't done anything." In this case here, I type, and then after one second, it shows what I type, which was quite different to Delay of one second.

[03:44] In that case, I would get each of these intermediate letters. Let's say you're doing something like a request to the server to do a search. You don't want to send a request for H, then a request for H-E, and then a request for H-E-L, etc. You want the request that means really what the user meant. He meant to send "Hello," and we know that because the user has done nothing for one second. Then we can emit the latest value.

[04:15] DebounceTime is comparable to Delay, and delayWhen is comparable to just Debounce. The name here is slightly different, but this is what they correspond to. That means that if you want to specify a function here, you could do this.

[04:38] You could return here an observable that says, "How much of event silence do you want to wait for." You could do something like that and take one, and this will do the same thing as we had before with debounceTime. It shows only four, and it's essentially the same idea as delayWhen.

[05:07] We use debounceTime and debounce to let you drop some emissions, while at the same time delaying the other emissions in the case emissions are happening too fast.