I have the following method which returns values asynchronously. return inventoryData
seems to fire before the rest of the values are populated. Here is the log:
I want to wait until all the values are retrieved before return inventoryData
is fired. How would I do that?
function checkInventoryUrl(url, size, code) {
var inventoryData = {};
return $http.get(url).then(function(response) {
var html = response.data;
// returns value instantly
inventoryData.url = url;
// parses html, takes a few seconds to return value
inventoryData.name = getProductName(html);
inventoryData.price = getPrice(html);
// returns promise
getProductQty(html, size).then(function(result) {
if(result) {
inventoryData.qtyAvailable = result;
console.log(inventoryData);
}
});
// returns promise
getProductMaxOrder(html, size).then(function(result) {
if(result) {
inventoryData.maxOrder = result;
console.log(inventoryData);
}
});
// returns promise
getProductSize(html, size).then(function(result) {
if(result) {
inventoryData.size = result;
console.log(inventoryData);
}
});
// fired straight away returns empty object
return inventoryData;
});
}