I have an items array that I would be getting from localStorage.
var items = JSON.parse($window.localStorage.selectedResources)['server'];
var arr = [];
var idsArray = [];
angular.forEach(items, function (item) {
idsArray.push(item.id);
});
Then I fire an API call ...
//Make the API call
ds.getBillInfo(idsArray)
.then(function(response){
var serversList = [];
for (var key in response) {
// iterate over response
The problem is if the items array is empty, so does the idsArray.
Then the error says Cannot read property 'then' of undefined.
What I want to do is even if the idsArray is empty , I want lines to execute inside the then block thinking as there is no promise.
How can I do that ?
EDIT
If I do $q.all([ds.getBillInfo(idsArray)]) then there is no error.
The getBillInfo() looks like:
this.getBillInfo = function(idsArray){
if(!idsArray.length) return;
var segmentUrl = '';
for(var i =0;i<idsArray.length;i++){
if(i != (idsArray.length-1))
segmentUrl += 'ids='+idsArray[i]+'&';
else
segmentUrl += 'ids='+idsArray[i];
}
return HttpWrapper.send('/api/bill?bill=t&'+segmentUrl, {"operation": 'GET'});
};
getBillInfoto always return a promisePromise.reject('empty')would work, and you'd end up in the catch, orPromise.resolve()to resolve