0

以下を参考にPromise処理を追加しました。
https://angular.io/tutorial/toh-pt6

例えば、以下のような処理で
this.http.getがエラーとなった場合、
「.catch」で指定した「handleError」が呼ばれることは確認できました。
たとえば特定のエラーの場合にgetHeroes()の呼びもとに返すことは可能なのでしょうか?

下記のような場合に★1処理をそのまま続行させたいです。

this.httpService.getHeroes()
  .then(response => {
    ★1
  })


private heroesUrl = 'api/heroes';  // URL to web api

constructor(private http: Http) { }

getHeroes(): Promise<Hero[]> {
  return this.http.get(this.heroesUrl)
             .toPromise()
             .then(response => response.json().data as Hero[])
             .catch(this.handleError);
}

private handleError(error: any): Promise<any> {
  console.error('An error occurred', error); // for demo purposes only
  return Promise.reject(error.message || error);
}

1 件の回答 1

2

handleErrorでrejectされたPromiseを返さなければ良いです。

if(特定エラー){
 const response = {}
 return response
}else{
    return Promise.reject(error.message || error);
}

then.catch.then
とチェインした時、catchが正常であれば、それに続くthenが処理されます。
現状は、Promise.rejectでエラーを返しているので、それに続くthenが処理されません。

1
  • hiroさま回答ありがとうございます。さっそくためいしてみたところ期待通りに動くことを確認できました!
    – kanko
    2017年10月23日 1:20

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。