jmdobry / angular-cache

angular-cache is a very useful replacement for the Angular 1 $cacheFactory.
http://jmdobry.github.io/angular-cache
MIT License
1.39k stars 156 forks source link

Saving a promise to cache #119

Closed mort3za closed 10 years ago

mort3za commented 10 years ago

I save a promise to cache, but when i want to use it from cache, it's undefined.

var my_model = dataModel.angCache('model');
// this returns a promise
var temp = dataLoader('my/model.json');
my_model.put('cachekey', temp);
console.log(my_model.get('cachekey'));  
// also calling my_model.get('cachekey').then(...) throws an error: 
// Cannot read property 'then' of undefined

I think before updating angular-cache it was working but now has issue.

mort3za commented 10 years ago

I tested version 2.3.1 and it works without error but version 2.3.7 seems buggy.

jmdobry commented 10 years ago

angular-cache < 2.3.6 never officially supported promises, only recently has support been added. Now when you put a promise into a cache, angular-cache calls .then on the promise and inserts the resolved value when the then callback fires.

@mort3za Try the following:

var temp = dataLoader('my/model.json');
my_model.put('cacheKey', temp);

temp.then(function () {
  console.log(my_model.get('cacheKey');
});