Matteo Defanti
Matteo Defanti

Reputation: 212

AngularJS getting out a return value from a Promise

I have this Angular JS Service:

'use strict';
app.factory('loggedService', ['$http', 'authService', 'customerService', function ($http, authService, customerService) {
    var out = [];

    if (authService.authentication.isAuth == false) {
        $location.path('/login');
        out = "effettuare login";
    }
    else {

        customerService.getCustomerAnagrafica().then(

            function (results) {

            out = results.data;

        }, function (error) {
            //alert(error.data.message);
        });
    }
    return { out: out };
}]);

What I would like to return the value of results.data.

I wanted to do something like the trick _this = this; and I tried to put that code after .then( with no success.

My goal is to geth the results.data out of that service.

to be called from a controller simply like this: $scope.myResults = loggedService.out;

Upvotes: 0

Views: 516

Answers (3)

Jon Z
Jon Z

Reputation: 516

You need to return a promise .. So your code.. and sorry I am writing this in the browser.. so I may have a typo..

'use strict';
app.factory('loggedService', ['$q','$http', 'authService', 'customerService', function ($q,$http, authService, customerService) {

  var deferred = $q.defer();

  if (authService.authentication.isAuth == false) {
    $location.path('/login');
    deferred.resolve("effettuare login");
  }
  else {
    customerService.getCustomerAnagrafica().then(
        function (results) {
        deferred.resolve(results.data)
    }, function (error) {
        //alert(error.data.message);
    });
  }
  return deferred;
}]);

The you can call loggedService.promise.then(function(data){ ....

Upvotes: 0

MinusFour
MinusFour

Reputation: 14423

You are creating a race condition by not working with the promise and using a closure instead. In other words, when you use your closure for other things your asynchronous job might have not populated the closure yet.

This is basically what you are doing:

var closure = 'not yet';

var getReady = function(){
  var func = function(){
      closure = 'i am ready.';
    }
  setTimeout(func, 1000);
};

getReady();
alert(closure);
//not ready yet (because it takes 1000 ms).

What you need to do is keep on working with the promise, in sequence.

    function constructHouse() {
        var house = new Promise(buildHouse);
        var paintedHouse = house.then(paintHouse);
        var cleanHouse = paintedHouse.then(cleanHouse);
        return cleanHouse;
    }
    //You can keep on working in sequence with the returned promise:
    constructHouse().then(sellHouse); 

Upvotes: 1

Joel Banzatto
Joel Banzatto

Reputation: 147

You could use an Observer but it is not the best way. Your factory should return the promise and your Controller should use it to interact in $scope.

Every time your service has to interact with scope, you must use the promises as well.

Upvotes: 1

Related Questions