2016-08-17 56 views
13

我有這個代碼什麼是觀察到相當於`Promise.reject`

return this.http.get(this.pushUrl) 
     .toPromise() 
     .then(response => response.json().data as PushResult[]) 
     .catch(this.handleError); 

我想用的observable代替Promise

我哪有錯誤返回給調用方法?

什麼與Promise.reject相等?

doSomeGet() { 
     console.info("sending get request"); 

     this.http.get(this.pushUrl) 
      .forEach(function (response) { console.info(response.json()); }) 
      .catch(this.handleError); 
    } 

    private handleError(error: any) { 
     console.error('An error occurred', error); 
     // return Promise.reject(error.message || error); 
    } 
} 

調用的方法是:

getHeroes() { 
    this.pushService 
     .doSomeGet(); 
     // .then(pushResult => this.pushResult = pushResult) 
     // .catch(error => this.error = error); 
} 
+0

上面的代碼與''Promise.reject''註釋掉工作完全正常。我不知道爲什麼。你有什麼想法嗎? – raj

回答