timeout
签名: timeout(due: number, scheduler: Scheduler): Observable
在指定时间间隔内不发出值就报错
示例
示例 1: 2.5秒后超时
import { of } from 'rxjs/observable/of';
import { concatMap, timeout, catchError } from 'rxjs/operators';
// 模拟请求
function makeRequest(timeToDelay) {
return of('Request Complete!').pipe(delay(timeToDelay));
}
of(4000, 3000, 2000)
.pipe(
concatMap(duration =>
makeRequest(duration).pipe(
timeout(2500),
catchError(error => of(`Request timed out after: ${duration}`))
)
)
)
/*
* "Request timed out after: 4000"
* "Request timed out after: 3000"
* "Request Complete!"
*/
.subscribe(val => console.log(val));
其他资源
- timeout
- 官方文档
源码:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/timeout.ts