2016-10-27 12 views
0

以下、ログインの詳細を再度確認してからhttpリクエストを再試行する必要のある401エラーに対して、Angular2の一般的なhttpエラー処理を追加しようとしています。複数のRX観測値を連鎖して1つの関数として返す方法

ログインが完了する前に機能が終了するため、再試行は機能しません。私は、2つ以上のオブザーバブルを連鎖させる方法が必要です。

Typescripts async/awaitを使用してこれを行う方法がわかりますが、これにはRXスタイルの修正が必要ですか?

request(url: string, options?: RequestOptionsArgs, retry: boolean = true): Observable<Response> { 
    return this.http.request(url, options) 
     .catch(initialError => { 

      console.log(initialError); 
      if (initialError.status === 401) { 
       // load login screen, and re-auth 
       let login = new LoginDetails(); 
       login.username = 'John'; 
       login.password = 'John'; 
       // this will call another observable to load a dialog form and get the credentials 

       this.apiAccountLoginPost(login) 
        .subscribe(data => { 
         // save session data 
         localStorage.setItem('session', data.toJSON()); 
         // retry the http request again 
         return this.request(url, options); // need to promote this 
        }, error => { 
         // process error 
         return Observable.throw(initialError); 
        }); 
       // TODO need to return the login retry as a Observable<Response> 

答えて

0
Observable.combineLatest(someObservable, anotherObservable).subscribe(response => { 
    console.log("this is array of responses: on 0 place you got response from 'someObservable, on 1 from 'anotherObservable'"); 
    console.log(response); 
}); 

しかし、これはあなたの状況ではそうではありません。私はあなたがあなたのオブザーバーを制御するためにnextまたはerrorメソッドを使用することができ、それをwithrxjs/add/operator/retryWhenまたはretry

をやるべきだと思う

private sendRequest(type: RequestType, url: string, optional?: RequestOptionsArgs, body?: any):Observable<Response> { 
return Observable.create((subscriber: any) => { 
    someRequest.subscribe(val => { 
     console.log("correct one"); 
     subscriber.next("push something"); 
    }, (err) => { 
     console.log("error handling"); 
     subscriber.next("push some error"); 
    }); 
}); 
} 
関連する問題