3
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 reset and process the result:
var dealerships = getDealerships('sul');
dealerships.done(function(data){
//...
});
The problem is that I need to implement a simple cache system to prevent the server from being consulted if the user chooses the same region:
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 and not a string.
How to solve the problem?
I made a similar solution in angular. Several calls that were made in HTTP I remade with a Promise that returned the same object Sponse, worked well
– Paulo Gustavo
It is worth remembering that you have to be careful so that the
fail
be called, otherwise there will be a silent failure in the execution ofPromise
.– Gabriel Katakura
Sergio, the documentation does not work in IE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Browser_compatibility and I really need to consider IE.
– Filipe Moraes
See if that answer help you.
– Ivan Ferrer
@Filipemoraes put together a solution for older, old-fashioned browsers (which I still prefer today).
– Sergio