JSFiddle - React, Tailwind, and code Playground
JavaScript
var IDs = ["1", "2", "1", "3", "1"];
//ID "1" is repeated
//data for "1" should should require ajax get the first time
//subsequent processing should get data for "1" from dataCache
var dataCache = {};
function getData(ID) {
if (ID in dataCache) {
console.log("cache hit for ID " + ID);
return dataCache[ID];
} else {
console.log("retrieving data for ID " + ID);
return $.getJSON("http://jsfiddle.net/echo/jsonp/?callback=?", {
id: ID
}, function(response) {
console.log("caching data for ID " + ID);
dataCache[ID] = response;
});
}
}
for (var i=0; i<IDs.length; i++) {
$.when( getData(IDs[i]) ).then( function(result) {
console.log(result);
});
}