Currently, I am working with Typescript 1.9
and utilizing RxJS 5
. My goal is to create an observable that will emit a single value: true
if any of the values emitted by the inner Observable<number>
belong to a predetermined array of numbers, and false
otherwise. Below is the code snippet I have implemented:
let lookFor = [2,7]; // Known values to search for
Observable.from([1,2,3,4,5]) // Inner observable emits these dynamic values
.first(
(d:number) => lookFor.find(id=>id===d)!==undefined,
()=>true
)
.subscribe(
res => console.log('Result: ',res),
err => console.error(err),
() => console.log('Complete')
);
The above code functions as intended, outputting:
Result: true (since inner observable emits 2, which is found in
lookFor
)Complete
If starting with Observable.from([8,9])
, the desired outcome would be Result: false
to indicate no overlap with lookFor
. However, instead, the error handler is activated:
Object {name:"Empty Error", stack:""}
What is the correct approach to ensure my observable emits true
upon finding a match, but emits false
if no match is found throughout the stream?