timeout

签名: timeout(due: number, scheduler: Scheduler): Observable

在指定时间间隔内不发出值就报错

timeout - 图1

示例

示例 1: 2.5秒后超时

( jsBin |
jsFiddle )

  1. import { of } from 'rxjs/observable/of';
  2. import { concatMap, timeout, catchError } from 'rxjs/operators';
  3. // 模拟请求
  4. function makeRequest(timeToDelay) {
  5. return of('Request Complete!').pipe(delay(timeToDelay));
  6. }
  7. of(4000, 3000, 2000)
  8. .pipe(
  9. concatMap(duration =>
  10. makeRequest(duration).pipe(
  11. timeout(2500),
  12. catchError(error => of(`Request timed out after: ${duration}`))
  13. )
  14. )
  15. )
  16. /*
  17. * "Request timed out after: 4000"
  18. * "Request timed out after: 3000"
  19. * "Request Complete!"
  20. */
  21. .subscribe(val => console.log(val));

其他资源


:file_folder: 源码:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/timeout.ts