2017-09-19 7 views
0

私は初心者ですが、この質問は愚かでも繰り返しでもかまいません。申し訳ありません。 !条件が満たされない限り、HTTPリクエストを繰り返してください。

私の目標は、= response.Done限りのWeb APIにリクエストを送信する場合はtrueであり、これは私がのOnInitイベントでサービスから関数を呼び出す私のコードです:

export class FlightListComponent implements OnInit { 
 

 
    flightSearchResult: FlightSearchResult; 
 

 
    constructor(private service: FlightService) { 
 
    } 
 

 
    ngOnInit() { 
 

 
    let guid = Guid.newGuid(); 
 

 
    this.service.getAll(guid).subscribe(response => { 
 

 
     if (response.Done != true) { 
 
     this.serveData(response); 
 
     // this.service.getAll(guid) .... 
 
     } 
 

 
    }); 
 
    }

私はまた、私はそれらをメイン変数にserveData()でマージする必要があるいくつかのデータを取得します。しかし、私はこの場合、私がgetAll()を呼び出す必要があるという観察可能な方法で作業するべきだと思う。Doneが真ではなく、イベントは各要求をマージすることなく最後にすべてのデータを与える。どのようにして各Observableのすべての値が単一のObservableにマージされたか

解決策はありますか?

答えて

1

これは動作するはずです:

ngOnInit() { 
    let guid = Guid.newGuid(); 
    this.getAll(guid);  
} 

getAll(guid: Guid) { 
    this.service.getAll(guid).subscribe(response => { 
     if (response.Done != true) { 
      this.serveData(response); 
      this.getAll(guid); 
     } 
    }); 
} 

を本当にこの背後にあるロジックを知らないが、あなたが呼び出しているAPIをオーバーロードする可能性があるため、あなたはおそらくネストされたコールをデバウンスすべきです。私はあなたが再試行ロジックを探していると思う

0

は、サンプルがbelow-ある

ngOnInit(){

let guid = Guid.newGuid(); 

this.service.getAll(guid).subscribe(response => { 

    if (response.Done != true) { 
    retryWaitTime = 10; 
    retryCount = 2; 
    counter++; 
    if (counter < retryCount) { 
     $timeout(function() { 
       this.serveData(response); 
       // this.service.getAll(guid) .... 
      , retryWaitTime); 
    } else { 
     counter = 0; 
     //return error response 
    } 

    } 

});