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!

2
  • How about returning results.data or whatever the property may be called? Commented Nov 19, 2015 at 8:24
  • I dont know, thats what Im looking for. Commented Nov 19, 2015 at 8:37

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

I think I used it without success. Let me check it!
SpringDataRestAdapter.process() already returns a promise, why creating another one? Edit: @devqon edited his answer to return the function.
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?
@JorgeBaumann you do have to use promises, because it is an async call
You can't, since it's an asynchronous operation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.