d3acc3a8bca0c44556c3e59d877ddaf8cf6da37cf382b09c04772d9f5072a3c52942f37834abb07e46f7ab18a171fa9e81f36b0ec7c3cac8c78a878e467452 4.1 KB

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