I am new to angular, in an application I am writing angular services for doing REST API call.
What I am doing like below:
App.service('dataService', function(){
this.getNewsAndTweets = function(){
var newsCall = $http({
method: 'GET',
url: '/api/news/'
});
var tweetsCall = $http({
method: 'GET',
url: '/api/tweets/'
});
$q.all(newsCall, tweetsCall).then(function(values) {
var news = values[0].data.results;
var tweets = values[1].data.results;
var a = doSomeThing(news);
var b = doSomeThingToo(tweets);
return [a, b];
});
}
}
As $q.all(newsCall, tweetsCall).then is an asynchronous call, I am getting null array in news when I am calling my service.
Inside my controller I am calling it like dataService.getNewsAndTweets() and getting null array, as far as my understanding as $q is an async call which is returning data later.
How can I resolve this issue?
Thanks in advance.