2016-03-30 1 views
2

をやり直します:2角度:ユーザーをAsumming</p> <p>は、各要求のために、内logedされています。私は、角度2を使用して実装したいと思いますいくつかの基本的な流れを持っていると これが流れているのrxjs HTTPリクエストから回復し、要求

  1. 要求が失敗した場合、成功の再失敗した要求
  2. を送信する場合、何かの非同期(バックグラウンドで再ログイン)
  3. を行う
  4. 、エラーをキャッチ

    が失敗するとエラーがスローされます。

    class MyHttpWrapper { 
    ctor(private http:Http) {} 
    
    get (url, options) { 
        //Do some pre request things with the options and url 
    
        return this.get(url,options) 
            .map(res => res.json()) 
            .catch((err, source) => { 
              // Here i want to reloging 
              someService.login().subscribe(res => 
              //Here i want to re-execute the original request and return it to the caller of the myHttpwrapper.get() caller 
             ).catch(err => 
               //return error to the caller of the myHttpWrapper.get() 
             )        
            } 
    } 
    

はどのように私はそれを行うことができますか?

答えて

4

私はそのような何かしようとするだろう:

return this.http.get(url,options) 
      .catch((err, source) => { 
      return someService.login() 
         .flatMap((res) => { 
         return this.http.get(url,options); 
         }); 
      }) 
      .map(res => res.json()); 

loginと第二getの両方にエラーがある場合、エラーがsubscribeメソッドの2番目のコールバックにスローされます。

+0

'map'演算子の使用を外部化することによってコードサイズを少し減らします... –

+0

' flatMap'の後の '.catch'はログイン時にエラーを捕捉するのでしょうか? – tubu13

+0

はい、それは 'flatMap'によって返された観測値に適用されるので、そのようなエラーをキャッチします。 –

関連する問題