Debian Codegraph @

node/rxjs/src/internal/operators/onErrorResumeNextWith.ts

raw
1 import { ObservableInputTuple, OperatorFunction } from '../types';
2 import { argsOrArgArray } from '../util/argsOrArgArray';
3 import { onErrorResumeNext as oERNCreate } from '../observable/onErrorResumeNext';
4
5 export function onErrorResumeNextWith<T, A extends readonly unknown[]>(
6 sources: [...ObservableInputTuple<A>]
7 ): OperatorFunction<T, T | A[number]>;
8 export function onErrorResumeNextWith<T, A extends readonly unknown[]>(
9 ...sources: [...ObservableInputTuple<A>]
10 ): OperatorFunction<T, T | A[number]>;
11
12 /**
13 * When any of the provided Observable emits an complete or error notification, it immediately subscribes to the next one
14 * that was passed.
15 *
16 * <span class="informal">Execute series of Observables, subscribes to next one on error or complete.</span>
17 *
18 * ![](onErrorResumeNext.png)
19 *
20 * `onErrorResumeNext` is an operator that accepts a series of Observables, provided either directly as
21 * arguments or as an array. If no single Observable is provided, returned Observable will simply behave the same
22 * as the source.
23 *
24 * `onErrorResumeNext` returns an Observable that starts by subscribing and re-emitting values from the source Observable.
25 * When its stream of values ends - no matter if Observable completed or emitted an error - `onErrorResumeNext`
26 * will subscribe to the first Observable that was passed as an argument to the method. It will start re-emitting
27 * its values as well and - again - when that stream ends, `onErrorResumeNext` will proceed to subscribing yet another
28 * Observable in provided series, no matter if previous Observable completed or ended with an error. This will
29 * be happening until there is no more Observables left in the series, at which point returned Observable will
30 * complete - even if the last subscribed stream ended with an error.
31 *
32 * `onErrorResumeNext` can be therefore thought of as version of {@link concat} operator, which is more permissive
33 * when it comes to the errors emitted by its input Observables. While `concat` subscribes to the next Observable
34 * in series only if previous one successfully completed, `onErrorResumeNext` subscribes even if it ended with
35 * an error.
36 *
37 * Note that you do not get any access to errors emitted by the Observables. In particular do not
38 * expect these errors to appear in error callback passed to {@link Observable#subscribe}. If you want to take
39 * specific actions based on what error was emitted by an Observable, you should try out {@link catchError} instead.
40 *
41 *
42 * ## Example
43 *
44 * Subscribe to the next Observable after map fails
45 *
46 * ```ts
47 * import { of, onErrorResumeNext, map } from 'rxjs';
48 *
49 * of(1, 2, 3, 0)
50 * .pipe(
51 * map(x => {
52 * if (x === 0) {
53 * throw Error();
54 * }
55 *
56 * return 10 / x;
57 * }),
58 * onErrorResumeNext(of(1, 2, 3))
59 * )
60 * .subscribe({
61 * next: val => console.log(val),
62 * error: err => console.log(err), // Will never be called.
63 * complete: () => console.log('that\'s it!')
64 * });
65 *
66 * // Logs:
67 * // 10
68 * // 5
69 * // 3.3333333333333335
70 * // 1
71 * // 2
72 * // 3
73 * // 'that's it!'
74 * ```
75 *
76 * @see {@link concat}
77 * @see {@link catchError}
78 *
79 * @param sources `ObservableInput`s passed either directly or as an array.
80 * @return A function that returns an Observable that emits values from source
81 * Observable, but - if it errors - subscribes to the next passed Observable
82 * and so on, until it completes or runs out of Observables.
83 */
84 export function onErrorResumeNextWith<T, A extends readonly unknown[]>(
85 ...sources: [[...ObservableInputTuple<A>]] | [...ObservableInputTuple<A>]
86 ): OperatorFunction<T, T | A[number]> {
87 // For some reason, TS 4.1 RC gets the inference wrong here and infers the
88 // result to be `A[number][]` - completely dropping the ObservableInput part
89 // of the type. This makes no sense whatsoever. As a workaround, the type is
90 // asserted explicitly.
91 const nextSources = argsOrArgArray(sources) as unknown as ObservableInputTuple<A>;
92
93 return (source) => oERNCreate(source, ...nextSources);
94 }
95
96 /**
97 * @deprecated Renamed. Use {@link onErrorResumeNextWith} instead. Will be removed in v8.
98 */
99 export const onErrorResumeNext = onErrorResumeNextWith;