class UrlProvider {
constructor(root) {
this.root = root;
}
getUrl() {
return this.root + "/api/Situations";
}
}
class Repository {
constructor(urlProvider) {
this.urlProvider = urlProvider;
}
getAsync() {
var self = this;
return httpGetAsync(self.urlProvider.getUrl()).then(function(d) {
return d.map(x => new Situation(x));
});
}
}
class PromiseCacher {
constructor(cache, promise) {
var self = this;
self.cache = cache;
self.promise = promise;
}
runAsync() {
var self = this;
return self.promise().then(function (d) {
self.cache.set(d);
return d;
});
}
}
var urlProvider = new UrlProvider(appsettings.url);
var repository = new Repository(urlProvider);
var cache = new Cache();
var promiseCacher = new PromiseCacher(cache, repository.getAsync);
promiseCacher.runAsync()
When debugging the above code, chrome will crash inside the getAsync function of the repository because self.urlProvider is undefined. Inside the repository getAsync function 'this' is referring to the instance of PromiseCacher which is what actually calls it.
What's going on?
Re-writing
var promiseCacher = new PromiseCacher(cache, ()=> repository.getAsync());
makes it work as I would expect.