Have written the Service to fetch the values from the promise and set the values to the Variables in my Service.
Retrives the promise and sets the value to the Variables in Service
getValueFromPromise =function(){
myPromise.then(function(res){
myArray =res;
console.log("Error");
});
}
This functionality has been coupled to my controller in such a way that it returns array
this.getItems = function(){
getValueFromPromise(); //Line1
console.log(myArray); //Line2
return myArray; //Line3
};
My question actually revolves around getItemsmodule where I invoke a getItemPromise() to set the value to my variable myArray.
- Question 1: What happens is that in the console I find myArray as [] empty at while loading the UI. Once I refresh my controller, I get the values in the myArray[]. I suspect this because in getItems() promise takes some time in evaluating the .get('URL') and since the operation done in asynchronous manner myArray values gets returned before promise was evaluated.Is that what I suspected right?
- Question 2: If my Suspection in Question1 was right, How to make Line3 wait till Line1 gets completed.Also Kindly enlighten me if I were anywhere wrong.