2016-08-18 20 views
8

मैं एकाधिक http कॉल का उपयोग कर स्थानीय संग्रहण में डेटा स्टोर करने की कोशिश कर रहा हूं। मैं फोर्कजॉइन का उपयोग तब तक प्रतीक्षा करता हूं जब तक कि सभी कॉल पूरी नहीं हो जातीं और फिर मैं अपनी Promise.then कॉल श्रृंखला फिर से शुरू करना चाहता हूं। मैं यह कैसे करु?आरएक्सजेएस को एक वचन के लिए अवलोकन करने योग्य

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); 
} 

उत्तर

13

आप एक Observable

की toPromise() विधि का उपयोग कर सकते हैं

https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/topromise.md

this.fetchData(cachesToUpdate).toPromise().then(...) 

संपादित: FriOne & Lyubimov रोमन ग में उल्लिखित omment,

import 'rxjs/add/operator/toPromise'; 
+6

'आयात' आरएक्सजे/एड/ऑपरेटर/समझौता करने के लिए मत भूलना "; –

संबंधित मुद्दे