Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2 observable timeout second parameter type

I am currently trying to make timeout() method of Angular 2 Observable work properly, but in all tutorials there is similair code, where the second argument of timeout() method is a simple Error:

return this.http.get('http://...')
                .timeout(2000, new Error('Timeout exceeded'));

But when I copy this code, TypeScript says that the second argument has invalid type and expects to see smth which implements interface IScheduler.

One of the ways to solve the problem is to make new class which implements IScheduler interface, but it has functionality I am not familiar with (now() method and Tasks).

Does anyone know, should I do it in this way or are there another ways to make things work? And what if instead of error I want to place some callback function?

like image 512
Артур Пипченко Avatar asked Mar 30 '17 06:03

Артур Пипченко


1 Answers

timeout does not take an Error as a parameter. If a timeout occurs it will throw a TimeoutError.

If you want to throw a particular type of error, you could use the timeoutWith operator to do something like this:

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!')));
like image 102
cartant Avatar answered Nov 13 '22 02:11

cartant