I'm having trouble understanding the concept of promises in AngularJS.
Here is a provider I have:
var packingProvider = angular.module('packingProvider',[]);
packingProvider.provider('packingProvider',function(){
return{
$get: function($http){
return{
getPackings: function(){
$http.post('../sys/core/fetchPacking.php').then(function(promise){
var packings = promise.data;
return packings;
});
}
}
}
}
});
This provides a method called getPackings()
, which will return an object
If I were to use this in my main application to retrieve the data, the call would be asynchronous, causing an issue where I need to 'wait' for the data:
var packings = packingProvider.getPackings();
console.log(packings); // undefined
Is there a way to achieve this without restructuring the process within my main controller?