Reputation: 16723
of
operators emits next
values. Eg of(1,2,3)
will emit values 1
,2
and 3
by calling next
of of
. Is there an operator which can call error
? I need this to simulate error handling in my unit test.
Upvotes: 1
Views: 25
Reputation: 2705
It is not strictly necessary to use an operator for this.
Below an example where every forth value will be an error:
const observable$ = Observable.create(observer => {
let n = 1;
const intervalId = setInterval(() => {
if (n < 4) {
observer.next(n);
n += 1;
} else {
observer.error("Error emitted");
}
}, 1000);
return () => clearInterval(intervalId);
});
observable$.subscribe({
next: x => console.log(x),
error: x => console.error(x)
});
However if you are looking for an operator the throwError
could be the one, which you are looking for. Details about it can be found here.
Upvotes: 1