How can I implement timeout for angular2+ http request

前端 未结 4 1645
迷失自我
迷失自我 2020-12-01 18:22

Here is just regular request looking like that:

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


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 18:31

    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);
    

提交回复
热议问题