2016-08-18 13 views
8

複数のhttp呼び出しを使用してローカルストレージにデータを格納しようとしています。すべての呼び出しが完了するまでforkJoinを使用してから、Promise.thenコールチェーンを再開します。これはどうすればいいですか?RxJSを観測可能に変換する

updateCache() { 
    var cachesToUpdate; 

    return this.fetchServerTimestamps() 
     .then(res => { 
      var localTimestamps = this.getLocalTimestamps(); 
      var serverTimestamps = this.getServerTimestamps(); 

      //Compare timestamps and decide which cache data types to update 
      cachesToUpdate = this.cacheTypes.filter(function (cacheType) { 
       return localTimestamps ? localTimestamps[cacheType.timestamp] != serverTimestamps[cacheType.timestamp] : true; 
      }); 
     }).then(res => { 
      //Request data and insert into cache 
      this.fetchData(cachesToUpdate) 
       .subscribe(res => { 
        res.forEach(function (item, index) { 
         this.insert(cachesToUpdate[index].messageType, JSON.stringify(item)); 
        }.bind(this)); 
       }); 
     }); 
} 

fetchData(cachesToUpdate): Observable<any> { 
    return forkJoin(cachesToUpdate.map(i => this.callservice.serverRead({ path: 'api/' + i.messageType }))); 
} 

insert(key: string, value: string, compress = true) { 
    localStorage.setItem(key, compress ? LZString.compress(value) : value); 
} 

答えて

関連する問題