RxJS conversion seen in promise

I am trying to store data in local storage with a few HTTP calls. I use forkJoin to wait until all calls are completed, and then I want to resume the Promise.then call Promise.then . How to do it?

 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); } 
+8
promise angular typescript observable rxjs
source share
1 answer

You can use the toPromise() method of Observable

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

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

Edit : as mentioned in a comment by FriOne and Roman Lyubimov, don't forget

 import 'rxjs/add/operator/toPromise'; 
+13
source share

All Articles