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.