Skip to content

Instantly share code, notes, and snippets.

@maxxcrawford
Created November 19, 2013 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxxcrawford/7552845 to your computer and use it in GitHub Desktop.
Save maxxcrawford/7552845 to your computer and use it in GitHub Desktop.
AJAX Call
var cache = {};
function getData( val ){
// Return a promise from the cache (if available)
// or create a new one (a jqXHR object) and store it in the cache.
var promise = cache[val];
if (!promise) {
promise = $.ajax('/foo/', {
data: { value: val },
dataType: 'json'
});
cache[val] = promise;
}
return promise;
}
$.when(getData('foo')).then(function(resp){
// do something with the response, which may
// or may not have been retrieved using an
// XHR request.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment