I'm implementing a small service for my angularJS app that makes an http request to my server for a couple of text posts. Here's my current implementation:
app.factory("postFetcher", ['$http', function($http){
var posts;
$http.get('https://cdn.contentful.com/spaces/k2mwszledbge/entries?access_token=afa8ba8572bb0232644d94c80cfd4ae01314cd0589b98551147aab50f7134e30')
.then(function(response){
posts = response.data.items;
});
return {
getList: function(){
return posts;
}
}
}]);
The problem with this is that the http request is not complete by the time that the posts array is returned. My intuition tells me to place the return function within the http .then function so that it is only returned once the request is complete, however this isn't allowed.
Is there a way I can delay the return of posts until the http request is complete?