简体   繁体   中英

angular js - Return promise instead data object

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!

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM