The code below returns a promise:
function getDealerships(region) {
return $.ajax({
method: "GET",
url: "/api/v1/dealerships?region=" + region
});
}
So I can wait for the server response and process the result:
var dealerships = getDealerships('sul');
dealerships.done(function(data){
//...
});
The problem is that I need to implement a simple caching system and thus prevent the server from being queried if the user chooses the same zone:
var dealershipsCache = [];
function getDealerships(region) {
if ( region in dealershipsCache )
return dealershipsCache[region];
return $.ajax({
method: "GET",
url: "/api/v1/dealerships?region=" + region
});
}
Note that if the region is cached, its value is returned, but what is expected is a promise rather than a string.
How to solve the problem?