2

Tried to minify my code so it is clear for you to see what i want to accomplish but not succeed.

What i want to accomplish is that when i do console.log($scope.tempData); i see the data instead of undefined so i can use it in the controller. I allready know that promises are the way to go, because of my question asked here on stackoverflow: $Scope variable is undefined

But what i don't know is how i can get a promise from $scope.getCube(); so i can just do $scope.getCube().then(function (data) { ... }); i found this: how to create asynchronous function but the answer was not clear enough for me.

/////////// Side Question ////////////

The code i provide on the bottom is what i have found on Github, not everything of that code is clear for me: Why is ClientService.dynamic() a promise? There is no q.defer in this.dynamic = function (params)?

//////////////////////////////////////////////

for a better reading of the code: JSFiddle

// CONTROLLER

$scope.buildObject = function () {

        $scope.getCube(); // 

        console.log($scope.tempData); //gives undefined, obvious because getCube is not done running yet.

    }

    $scope.getCube = function () {

        promise = ClientService.dynamic(params);

        promise.then(function (data) {

            $scope.tempData = data[0];

            return $scope.tempData;

        }

    }

/////////////////////////////////////////////////////

// DYNAMIC SERVICE

this.dynamic = function (params) {

    return qvCommService.send(createHyperCube).then(function (data) {

    }

}

//////////////////////////////////////////////////

// SEND SERVICE

send: function (msg) { 

    var deferred = $q.defer();
    var promise = deferred.promise;

    this.socket = new WebSocket(ws://*************);

    this.socket.addEventListener('open', function (e) {
        deferred.resolve("connected")

    });

    this.socket.send(angular.toJson(msg))

    return promise;
}
Community
  • 1
  • 1
Peter
  • 1,240
  • 2
  • 13
  • 22

1 Answers1

1

Basically you missed to return the promise from getCube function as it should return, & there after while calling getCube function you need to use .then function over it. So that you can get asynchronously evaluated value inside your success callback of .then

Code

$scope.getCube = function () {
    promise = ClientService.dynamic(params);
    promise.then(function (data) {
        $scope.tempData = data[0];
        return $scope.tempData;
    }
    return promise; //return promise object
};

$scope.buildObject = function () {
    $scope.getCube().then(function(){
        console.log($scope.tempData);
    })
}
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Really nice, thank you! Could you explain me why the `$scope.getCube = function()` or the `this.dynamic = function()` is a promise? `q.defer()` is not defined in this function? Are promises being inhered in a way? In this case from the `send: function()` – Peter Jan 14 '16 at 09:11
  • `send` method must have use `$http` call **OR** `$q` implementation..thats why it is working – Pankaj Parkar Jan 14 '16 at 09:21
  • I does have a $q implementation. See my original code `send: function (msg) {var deferred = $q.defer();var promise = deferred.promise;` I just don't really understand why `$scope.getCube = function()` or the `this.dynamic = function()` becomes a promise because the `send` is a promise – Peter Jan 14 '16 at 09:24
  • @Peter I'd highly recommend you to please go through http://www.webdeveasy.com/javascript-promises-and-angularjs-q-service/ link – Pankaj Parkar Jan 14 '16 at 09:27