Promise.race无法解决

时间:2016-09-26 23:53:43

标签: javascript angular promise ionic2 observable

我正在尝试在我的离子2应用程序上设置一个承诺超时,当服务器需要很长时间才能响应时。我的代码如下所示:

.
.
import 'rxjs/add/operator/toPromise';
.
.
  reqNextQuestion(selectedCategories:string[]) : Promise<any>{
    let url             = "xxxxx/user/id/1337";
    let questionPromise = this.http.get(url)
      .toPromise()
      .then(resp => resp.json());

    return Promise.race([questionPromise, this.timeOutReject(5000, 'Service Error')]);
  }

  timeOutReject(durr?: number, errMsg?: string) : Promise<any>{
    let currDurr   = durr   || this.timeOutDurr;
    let currErrMsg = errMsg || this.timeOutMsg;

    return new Promise(function(resolve, reject){
      setTimeout(() => reject(new Error(currErrMsg)), currDurr);
    });
  }

现在当我解雇以下内容时:

 this.reqNextQuestion(this.currCatSelection)
        .then(obj => {
           // NEVER REACH THIS POINT
         }).catch(err => {
           // ONLY GETTING HERE
         });

我从未收到服务器的回复。它像我之前定义的那样在5000毫秒之后跳转到catch块...

之前我测试过服务器,平均花了大约60毫秒来获得响应。

你知道问题是什么吗?

0 个答案:

没有答案
相关问题