RxJS throwError

2020-10-13 18:52 更新

創(chuàng)建一個 Observable,不向觀察者發(fā)出任何項目 發(fā)出錯誤通知。

throwError(error: any, scheduler?: SchedulerLike): Observable<never>

參量

錯誤 要傳遞給錯誤通知的特定錯誤。
調(diào)度器 可選的。 默認值為 undefined。  一個 SchedulerLike為計劃  錯誤通知的發(fā)出。

退貨

Observable<never>:錯誤可觀察:僅發(fā)出錯誤通知 使用給定的錯誤參數(shù)。

描述

只是發(fā)出“錯誤”,別無其他。

扔大理石圖

此靜態(tài)運算符可用于創(chuàng)建僅 發(fā)出錯誤通知。 它可以用來與其他 可觀測值,例如中的 mergeMap。

例子

發(fā)出數(shù)字7,然后發(fā)出錯誤

  1. import { throwError, concat, of } from 'rxjs';
  2. const result = concat(of(7), throwError(new Error('oops!')));
  3. result.subscribe(x => console.log(x), e => console.error(e));
  4. // Logs:
  5. // 7
  6. // Error: oops!

將數(shù)字映射并展平到序列'a','b','c',但會拋出錯誤2

  1. import { throwError, interval, of } from 'rxjs';
  2. import { mergeMap } from 'rxjs/operators';
  3. interval(1000).pipe(
  4. mergeMap(x => x === 2
  5. ? throwError('Twos are bad')
  6. : of('a', 'b', 'c')
  7. ),
  8. ).subscribe(x => console.log(x), e => console.error(e));
  9. // Logs:
  10. // a
  11. // b
  12. // c
  13. // a
  14. // b
  15. // c
  16. // Twos are bad

也可以看看

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號