I'm writing this function to use in my angular app to evaluate ng-src in an ng-repeat list. I need to make the calls synchronous so that the value is correct each time the function is called. The problem is:
Why does this code return a value:
var storage = firebase.storage();
var returnVal; //causes sync issues
var getImageUrl = function (time) {
storage.ref('images/' + time + '.jpg').getDownloadURL().then(function (url) {
returnVal = url;
});
return returnVal;
};
But this doesn't work:
var storage = firebase.storage();
var getImageUrl = function (time) {
var returnVal; //so that this is specific to the function
storage.ref('images/' + time + '.jpg').getDownloadURL().then(function (url) {
returnVal = url; //I simply want to return the value of 'url'
});
return returnVal;
};
Any ideas how I can make the getImageUrl() function return the url from the .then?
This is the documentation link: https://firebase.google.com/docs/storage/web/download-files
Eventually I'll turn this into a $scope function to use similar to this:
<div ng-repeat="message in messages">
<img ng-src="{{ getImageUrl(message.time) }}">
</div>