How to implement HTTP timeout after the latest version of Rxjs?

I used to set the timeout on my angular2 mailbox as shown below:

this.http.post('myurl',
      body, {headers: headers})
      .timeout(2000, new Error('Timeout exceeded'))
      .map(res => res.json())
      .subscribe((stuff) => {
         //Do stuff
      }, (errorResponse: any) => {
        //Manage error
      });

But with the latest version of Rjxs (5.0.1) this is no longer valid.

The wait time should be observable as the first parameter and does not accept a “new error”, how do I do this / write?

In advance for your help

Note. When I delete the “new error (...)”, then my code is valid, but at runtime I encountered the following error

Error: unclean (in promise): TypeError: _this.http.post (...). timeout is not a TypeError function: _this.http.post (...). timeout is not a function

+4
source share
2

, :

import 'rxjs/add/operator/timeout';

this.http.post('myurl',
  body, {headers: headers})
  .timeout(2000)
  .map(res => res.json())
  .subscribe((stuff) => {
     //Do stuff
  }, (errorResponse: any) => {
    //Manage error
  });

****** Angular >= 4.3 Rxjs >= 5.2.2 ******

RXJS 5.2 timeout pipe. , lettable ( , lettable).

Angular 4.3 HttpClientModule, - HttpModule.

:

import {timeout} from 'rxjs/operators/timeout'; 

let headers: HttpHeaders = new HttpHeaders();
headers.append('Content-Type', 'application/json');

let body = {something: 'my_value'};

this.http.post('myurl',
  body, {headers: headers})
  .pipe( 
     timeout(2000)
  )
  .subscribe((stuff: any) => {
     //Do stuff
  }, (errorResponse: HttpErrorResponse) => {
    //Manage error
  });
+7

import 'rxjs/add/operator/timeout';

Fyi, ,

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/timeout';
...
0

Source: https://habr.com/ru/post/1664826/


All Articles