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:

[image: enter image description here]

I want to wait until all the values are retrieved before return 
inventoryData is fired. How would I do that?

Here is my code so far:

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;
  });
}

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to