angular 2 可观察超时第二个参数类型
angular 2 observable timeout second parameter type
我目前正在尝试使 Angular 2 Observable 的 timeout() 方法正常工作,但在所有教程中都有类似的代码,其中第二个参数timeout() 方法是一个简单的错误:
return this.http.get('http://...')
.timeout(2000, new Error('Timeout exceeded'));
但是当我复制这段代码时,TypeScript 说第二个参数的类型无效,并希望看到实现接口 IScheduler 的 smth。
解决问题的方法之一是制作新的 class 实现 IScheduler 接口,但它具有我不熟悉的功能 (now()方法和 任务).
有谁知道,我应该这样做还是有其他方法可以使事情正常进行?如果我想放置一些回调函数而不是错误怎么办?
timeout
does not take an Error
as a parameter. If a timeout occurs it will throw a TimeoutError
.
如果你想抛出特定类型的错误,你可以使用 timeoutWith
operator 来做这样的事情:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/timeoutWith';
return this.http
.get('http://...')
.timeoutWith(2000, Observable.throw(new Error('Boom!')));
我目前正在尝试使 Angular 2 Observable 的 timeout() 方法正常工作,但在所有教程中都有类似的代码,其中第二个参数timeout() 方法是一个简单的错误:
return this.http.get('http://...')
.timeout(2000, new Error('Timeout exceeded'));
但是当我复制这段代码时,TypeScript 说第二个参数的类型无效,并希望看到实现接口 IScheduler 的 smth。
解决问题的方法之一是制作新的 class 实现 IScheduler 接口,但它具有我不熟悉的功能 (now()方法和 任务).
有谁知道,我应该这样做还是有其他方法可以使事情正常进行?如果我想放置一些回调函数而不是错误怎么办?
timeout
does not take an Error
as a parameter. If a timeout occurs it will throw a TimeoutError
.
如果你想抛出特定类型的错误,你可以使用 timeoutWith
operator 来做这样的事情:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/timeoutWith';
return this.http
.get('http://...')
.timeoutWith(2000, Observable.throw(new Error('Boom!')));