如何超时angular2 http请求 [英] How to timeout angular2 http request

查看:1333
本文介绍了如何超时angular2 http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是常规请求:

 this.people = http.get('http://localhost:3000/users')
      .map(response => response.json());
  }

有没有办法实现/超时?

Is there any way to dealy/timeout that?

推荐答案

您可以利用observables的 timeout 运算符,如下所述:

You can leverage the timeout operator of observables, as described below:

return this.http.get('http://api.geonames.org/postalCodeSearchJSON',
          { search: params })
    .retryWhen(error => error.delay(500))
    .timeout(2000, new Error('delay exceeded')) // <------
    .map(res => res.json().postalCodes);

有关详细信息,请参阅此文章(重试支持部分):

See this article for more details (with the section "Retry support"):

  • http://restlet.com/blog/2016/04/18/interacting-efficiently-with-a-restful-service-with-angular2-and-rxjs-part-3/

这篇关于如何超时angular2 http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆