1

im working with angular.

In my service.js I'm trying to return some values but instead data I get the promise item with some $$variables & the data Im looking for.

Problem is I can't work with data since it's a promise. How can I get only the data object?

function loadLinks(respuesta,link) {
  return SpringDataRestAdapter.process(respuesta, link)
    .then(function (results) {
      return results;
    });
}

Im using Spring Data Rest. I copied this from another service which was working, but this one is not working well.

Any help?

Thanks you!

Baumannzone
  • 760
  • 2
  • 19
  • 38

1 Answers1

2

If you don't do any additional logic, you can just return the function which already is a promise:

function loadLinks(respuesta,link) {
    return SpringDataRestAdapter.process(respuesta, link);
}

And where you use it:

myService.loadLinks(respuesta, link).then(function(result) {
    $scope.results = result;
}, function() {
    // fail
});;

If you do want to have additional logic, you can use the $q service:

function loadLinks(respuesta,link) {
    var deferred = $q.defer();
    SpringDataRestAdapter.process(respuesta, link)
        .then(function (results) {
            // do something more
            console.log(results);
            deferred.resolve(results);
        }, function (error) {
            deferred.reject();
        });;

    return deferred.promise;
}
devqon
  • 13,818
  • 2
  • 30
  • 45
  • I think I used it without success. Let me check it! – Baumannzone Nov 19 '15 at 08:26
  • `SpringDataRestAdapter.process()` already returns a promise, why creating another one? **Edit**: @devqon edited his answer to return the function. – Ben Kauer Nov 19 '15 at 08:28
  • Seems like yes, @iWörk , because im using `.then()`. Anyway, not getting the correct answer. Do you know how to "process" it to get only data instead a promise item? – Baumannzone Nov 19 '15 at 08:30
  • 2
    @JorgeBaumann you do have to use promises, because it is an async call – devqon Nov 19 '15 at 08:31
  • 1
    You can't, since it's an asynchronous operation. – Ben Kauer Nov 19 '15 at 08:31
  • Yaaay! It works, with the 1st solution @devqon . Now im getting only data instead promise item. Thanks you very much! – Baumannzone Nov 19 '15 at 08:47
  • Please avoid the [deferred antipattern](http://stackoverflow.com/q/23803743/1048572) (in the second part of your answer)! – Bergi Nov 20 '15 at 10:15