When debugging, I have break points and it shows both axios calls and then goes straight to the end of the for loop to return an empty array instead of going to the .then(response => { which would push the json object into product array. How can I make the function wait for the pushes into the array before returning?
function getProduct(appId, method, productId, skuType, catalog, descriptionTypes, categories,
manufacturer, displayTemplate, accessoryMax, categorizeAccessories, skuNumberType, skuNumberVal,
mfgName, partNumber) {
var products = [];
for(var i = 0; i < appId.length; i++){
console.log("begining of i: " + i);
var jsonObj = {};
axios.get("http://ws-na1.spexlive.net/service/rest/catalog", {
params: {
appId : appId[i],
method: method,
productId: productId,
skuType: skuType,
catalog: catalog,
descriptionTypes: descriptionTypes,
categories: categories,
manufacturer: manufacturer,
displayTemplate: displayTemplate,
accessoryMax: accessoryMax,
categorizeAccessories: categorizeAccessories,
skuNumberType: skuNumberType,
skuNumberVal: skuNumberVal,
mfgName: mfgName[i],
partNumber: partNumber[i]
}
})
.then(response => {
console.log(response);
console.log("Etilize product info call successfull.");
if( parser.validate(response.data) === true) { //optional (it'll return an object in case it's not valid)
jsonObj = parser.parse(response.data,options);
}
// Intermediate obj
var tObj = parser.getTraversalObj(response.data,options);
jsonObj = parser.convertToJson(tObj,options);
console.log(jsonObj);
products.push(jsonObj);
// console.log(isPushed);
console.log(products);
console.log("i: " + i);
console.log("appId.length: " + appId.length);
})
.catch(error => { //catch error from Geohash using DynamoDB to query
console.log(error)
("Oops! Something went wrong with getting Product specs.")
})
console.log("Ending of loop");
} // end of for loop
return products;
}