Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

throwError.ts 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Observable } from '../Observable';
  2. import { SchedulerLike } from '../types';
  3. import { Subscriber } from '../Subscriber';
  4. /**
  5. * Creates an Observable that emits no items to the Observer and immediately
  6. * emits an error notification.
  7. *
  8. * <span class="informal">Just emits 'error', and nothing else.
  9. * </span>
  10. *
  11. * ![](throw.png)
  12. *
  13. * This static operator is useful for creating a simple Observable that only
  14. * emits the error notification. It can be used for composing with other
  15. * Observables, such as in a {@link mergeMap}.
  16. *
  17. * ## Examples
  18. * ### Emit the number 7, then emit an error
  19. * ```ts
  20. * import { throwError, concat, of } from 'rxjs';
  21. *
  22. * const result = concat(of(7), throwError(new Error('oops!')));
  23. * result.subscribe(x => console.log(x), e => console.error(e));
  24. *
  25. * // Logs:
  26. * // 7
  27. * // Error: oops!
  28. * ```
  29. *
  30. * ---
  31. *
  32. * ### Map and flatten numbers to the sequence 'a', 'b', 'c', but throw an error for 2
  33. * ```ts
  34. * import { throwError, interval, of } from 'rxjs';
  35. * import { mergeMap } from 'rxjs/operators';
  36. *
  37. * interval(1000).pipe(
  38. * mergeMap(x => x === 2
  39. * ? throwError('Twos are bad')
  40. * : of('a', 'b', 'c')
  41. * ),
  42. * ).subscribe(x => console.log(x), e => console.error(e));
  43. *
  44. * // Logs:
  45. * // a
  46. * // b
  47. * // c
  48. * // a
  49. * // b
  50. * // c
  51. * // Twos are bad
  52. * ```
  53. *
  54. * @see {@link Observable}
  55. * @see {@link empty}
  56. * @see {@link never}
  57. * @see {@link of}
  58. *
  59. * @param {any} error The particular Error to pass to the error notification.
  60. * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} to use for scheduling
  61. * the emission of the error notification.
  62. * @return {Observable} An error Observable: emits only the error notification
  63. * using the given error argument.
  64. * @static true
  65. * @name throwError
  66. * @owner Observable
  67. */
  68. export function throwError(error: any, scheduler?: SchedulerLike): Observable<never> {
  69. if (!scheduler) {
  70. return new Observable(subscriber => subscriber.error(error));
  71. } else {
  72. return new Observable(subscriber => scheduler.schedule(dispatch, 0, { error, subscriber }));
  73. }
  74. }
  75. interface DispatchArg {
  76. error: any;
  77. subscriber: Subscriber<any>;
  78. }
  79. function dispatch({ error, subscriber }: DispatchArg) {
  80. subscriber.error(error);
  81. }