1 import { Observable } from '../Observable';
2 import { SchedulerLike } from '../types';
3 import { Subscriber } from '../Subscriber';
6 * Creates an Observable that emits no items to the Observer and immediately
7 * emits an error notification.
9 * <span class="informal">Just emits 'error', and nothing else.
14 * This static operator is useful for creating a simple Observable that only
15 * emits the error notification. It can be used for composing with other
16 * Observables, such as in a {@link mergeMap}.
19 * ### Emit the number 7, then emit an error
21 * import { throwError, concat, of } from 'rxjs';
23 * const result = concat(of(7), throwError(new Error('oops!')));
24 * result.subscribe(x => console.log(x), e => console.error(e));
33 * ### Map and flatten numbers to the sequence 'a', 'b', 'c', but throw an error for 2
35 * import { throwError, interval, of } from 'rxjs';
36 * import { mergeMap } from 'rxjs/operators';
38 * interval(1000).pipe(
39 * mergeMap(x => x === 2
40 * ? throwError('Twos are bad')
43 * ).subscribe(x => console.log(x), e => console.error(e));
55 * @see {@link Observable}
60 * @param {any} error The particular Error to pass to the error notification.
61 * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} to use for scheduling
62 * the emission of the error notification.
63 * @return {Observable} An error Observable: emits only the error notification
64 * using the given error argument.
69 export function throwError(error: any, scheduler?: SchedulerLike): Observable<never> {
71 return new Observable(subscriber => subscriber.error(error));
73 return new Observable(subscriber => scheduler.schedule(dispatch, 0, { error, subscriber }));
77 interface DispatchArg {
79 subscriber: Subscriber<any>;
82 function dispatch({ error, subscriber }: DispatchArg) {
83 subscriber.error(error);