I'm puzzled by the results of the code below. According to the take(3) function, processing should stop after rendering 10, but I'm still seeing 5, 6, and 9 from the tap operator. Please see the output and code snippet below for reference.
- Tapped value null
- Tapped value 20
- Rendering Item 20
- Tapped value 15
- Rendering Item 15
- Tapped value 10
- Rendering Item 10
- Completed
- Tapped value 5
- Tapped value 6
- Tapped value 9
of(null, 20, 15, 10, 5, 6, 9)
.pipe(
tap(val => console.log(`Tapped value ${val}`)),
filterNil(),
take(3)
)
.subscribe(
item => console.log(`Rendering Item ${item}`),
err => console.log(err),
() => console.log('Completed')
);
}
const filterNil = () => (source: Observable<any>) =>
new Observable(observer => {
return source.subscribe({
next(value) {
if (value !== undefined && value !== null) {
observer.next(value);
}
},
error(error) {
observer.error(error);
},
complete() {
observer.complete();
}
});
});