that the service should return the promise, and not return the elements
populateData: function(data) { var deferred = $q.defer(); var items = []; for (i = 0; i < data.length; i++) { items.push(data[i]); } deferred.resolve(items); return deferred.promise; }
you may not need it, although you could do
var items = EventService.populateData(result);
usually promises are used if you do something asynchronously. Like an API call and waiting for a response. In these cases, the answer may take seconds to complete THEN, after which the .then function will be called. in your case, if you make this function a promise, it will be called almost immediately
EDIT: here is a link to $ q AngularJS Documentation : API: $ q
fwhenin
source share