I have a function in my codebase that is responsible for loading HTML templates asynchronously:
loadTemplate: function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(_.template(xhr.responseText));
} else {
reject(xhr.responseText);
}
}
};
xhr.onerror = function(error) {
reject(error);
};
xhr.send(null);
});
}
I am now seeking advice on how to enhance this function by implementing response caching within the user's browser. Any suggestions or tips would be greatly appreciated!