Would it be possible to create an ajax function without duplicating it? Passing different parameters, which are locations to various files. Then utilizing the promise to combine them into a single object, possibly incorporating the spread operator. Is this achievable?
var myFunctionCalls = 0;
let promiseAjax = new Promise(function(resolve, reject) {
//Could we potentially use a for loop to determine how many times the loadDoc function was called and then push the results to an array?
function loadDoc(location) {
myFunctionCalls++;
console.log("loadDoc was called: " + myFunctionCalls);
var xyz = new XMLHttpRequest();
xyz.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
resolve(this.responseText);
}
};
xyz.open("GET", location, true);
xyz.send();
}
loadDoc("/_js/someitems.json");
loadDoc("/_js/someMoreItems.json");
})
// Combine the responses into one object using spread operators
promiseAjax.then(function(fromResolve){
var newObj = JSON.parse(fromResolve);
console.log(newObj);
})