What is the difference between skipWhile and filter operators?
const source = interval(1000);
const example = source.pipe(skipWhile(val => val < 5));
const subscribe = example.subscribe(val => console.log(val));
const source = interval(1000);
const example = source.pipe(filter(val => val > 5));
const subscribe = example.subscribe(val => console.log(val));
The difference is that upon its expression evaluating to false
, skipWhile changes over to mirroring its source observable - so it will cease to filter out any further values.
For example:
Observable.from([1,2,3,4,5])
.pipe(filter(val => val % 2 == 0)) // filters out odd numbers
.subscribe(val => console.log(val)); // emits 2,4
Observable.from([1,2,3,4,5])
.pipe(skipWhile(val => val % 2 == 1)) // filters odd numbers until an even number comes along
.subscribe(val => console.log(val)); // emits 2,3,4,5
Observable.from(["Citizen 1","Citizen 2","President","Citizen 3","Citizen 4"])
.pipe(filter(val => val == "President")) // Only let the president into the bunker.
.subscribe(val => console.log(val)); // emits President
Observable.from(["Citizen 1","Citizen 2","President", "Citizen 3","Citizen 4"])
.pipe(skipWhile(val => val != "President")) // Let the citizens enter the bunker, but only after the president enters.
.subscribe(val => console.log(val)); // emits President, Citizen 3, Citizen 4
skipWhile
operator will ignore the emissions until the specified condition becomes false, but after that, it will continue to take values from the source observable as is.
const randomNumbersLessThanEqualToTen = interval(1000).pipe(map((num) => {
const randomNumber = Math.floor(Math.random()*num);
console.log('Random Number Generated', randomNumber);
return randomNumber;
}), skipWhile(num => num < 10));
randomNumbersLessThanEqualToTen.subscribe((number) => {
console.log('Number not skipped', number);
});
o/p
Random Number Generated 6
Random Number Generated 0
Random Number Generated 5
Random Number Generated 0
Random Number Generated 5
Random Number Generated 11
Number not skipped 11
Random Number Generated 6
As soon as 11 (i.e. num < 10 === false) got generated and emitted all the generated numbers were taken and printed.
Here is an example of filter
:
const randomNumbers = interval(1000).pipe(map((num) => {
const randomNumber = Math.floor(Math.random() * num);
console.log('Random Number Generated', randomNumber);
return randomNumber;
}), filter(num => num > 10));
randomNumbers.subscribe((number) => {
console.log('Number is greater than 10 -->', number);
});
o/p
Random Number Generated 7
Random Number Generated 11
Number is greater than 10 --> 11
Random Number Generated 13
Number is greater than 10 --> 13
Random Number Generated 13
Number is greater than 10 --> 13
Random Number Generated 3
Random Number Generated 2
So above output simply showcased that filter simply filters the emissions on the basis of the condition specified and it filters throughout the lifetime of observable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With