Hi Sander,
Many thanks for your reply. I implemented your code changes and now I have
the following
*inventoryCheckerCtrl.js*
vm.submitStockRequest = function() {
StockCheckerService.checkInventory().then(function(inventoryData) {
// this seems to fire first and doesn't wait for the service to
return a result
vm.inventoryResultObject.qtyAvailable = inventoryData.qtyAvailable;
vm.inventoryResultObject.maxOrder = inventoryData.maxOrder;
vm.inventoryResultObject.size = inventoryData.size;
vm.inventoryResultObject.name = inventoryData.name;
vm.inventoryResultObject.price = inventoryData.price;
vm.inventoryResultObject.url = inventoryData.url;
});
*inventoryCheckerService.js*
function checkInventory() {
var inventoryRequestObject = DataService.getFormData();
var store = inventoryRequestObject.store;
var code = inventoryRequestObject.code;
var size = inventoryRequestObject.size;
// Check store
if(store === 'nike') {
return checkStore(code, size);
}
}
function checkStore(code, size) {
var url = 'http://www.nike.co.uk/search?q=' + code;
return storeService.checkInventory(url, size, code);
}
*storeService.js*
function checkInventory(url, size, code) {
var inventoryData = {
'qtyAvailable': 0,
'maxOrder': 0,
'size': 0,
'name': '',
'price': '',
'url': '',
'code': code
return $http.get(url).then(function(response) {
var html = response.data;
inventoryData.url = url;
inventoryData.name = getProductName(html);
inventoryData.price = getProductPrice(html);
return $q.all([
getProductQty(html, size).then(function(result) {
if(result) {
inventoryData.qtyAvailable = result;
}
}),
getProductMaxOrder(html, size).then(function(result) {
if(result) {
inventoryData.maxOrder = result;
}
}),
getProductSize(html, size).then(function(result) {
if(result) {
inventoryData.size = result;
}
})
]).then(function(result) {
// everything is returned here correctly
console.log(inventoryData);
return inventoryData;
})
});
};
]
There is still a delay in retrieving the name, price, qtyAvailable,
maxOrder and size.
inventoryResultObject always returns null in inventoryCheckerCtrl.js.
Thanks for your time.
Please let me know if you need any further details.
On Monday, 27 February 2017 04:22:08 UTC, Sander Elias wrote:
>
> try this:
>
> 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
> return $q.all([
> getProductQty(html, size).then(function (result) {
> if (result) {
> inventoryData.qtyAvailable = result;
> console.log(inventoryData);
> }
> }),
> getProductMaxOrder(html, size).then(function (result) {
> if (result) {
> inventoryData.maxOrder = result;
> console.log(inventoryData);
> }
> }),
> getProductSize(html, size).then(function (result) {
> if (result) {
> inventoryData.size = result;
> console.log(inventoryData);
> }
> })
> ])
> .then(function dumm() {
> return inventoryData;
> })
> });
> }
>
> You are returning your result (inventoryData) before your promises are
> done.
> Regards
> Sander
>
>
--
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.