angular 2 observable timeout second parameter type

11,093

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!')));
Share:
11,093
A.Pipchenko
Author by

A.Pipchenko

Updated on June 19, 2022

Comments

  • A.Pipchenko
    A.Pipchenko almost 2 years

    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?