1

And on top of this, why are $scope values set BEFORE $q.all is called completely ignored?

Function within main controller:

$scope.apply = function (security) {

    var entity = shareDataService.getModalEntity();
    var depot = shareDataService.getModalDepot();
    $scope.loaded = false;
    var myDataPromise;

    if (entity == "NULL") {
        myDataPromise = getDataService.getDataFromREST(security);
    } else {
        myDataPromise = $q.all(getDataService.keepICorrect(security));
    };
    myDataPromise.then(function () {


                 //DO STUFF


}, function errorCallback(response) {

                    //DO MORE STUFF

    });
}

And my keepICorrect() function in my service (which is a closure):

 keepICorrect: function (security) {

        var promises = [];
            for (var i = 0 ; i < entity.length; i++) {
                promises.push(this.getDataFromREST(security, i));
            }
         return promises;
    },

However when the $scope.apply() function is executed, nothing happens. Boolean $scope.loaded does not activate on the scope and no exception is thrown (which is what I was expecting). Why is this?

I've edited the code and made my controller function Apply check if entity = "NULL", and this seems to have solved the issue of whether it is an array or not. Still doesn't answer my question as to why if q.all is not returned an array of promises, does nothing happen in the function it is called in, even if before q.all is called.

notAChance
  • 1,360
  • 4
  • 15
  • 47
  • What is `entity` in that `keepICorrect()` function? The parameter is `security`, and `security` is used in the loop. – Pointy Dec 02 '15 at 16:25
  • `entity` is an array which is declared and fetched in the service. `security` is passed through to my `getDataFromREST()` function within my for loop. – notAChance Dec 02 '15 at 16:26
  • Shouldn't it be `return $q.all` ? – Oli Dec 02 '15 at 16:31
  • Within my `$scope.apply` ? I wasn't aware of this, but it makes no difference (i just tried). – notAChance Dec 02 '15 at 16:32
  • 4
    `if (entity === Array)` compares `entity` to the global `Array` constructor function. It doesn't test to see if `entity` is an array. – Pointy Dec 02 '15 at 16:34
  • `if(entity.constructor === Array)` makes no difference. – notAChance Dec 02 '15 at 16:39
  • 1
    This surely IS how you check if a var is an array... http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript – notAChance Dec 02 '15 at 16:43
  • I am having a hard time understanding when you would expect an exception to be thrown. First of all, what value does `entity` hold (depending on that $q.all() might never be called...)? Also, where do you throw an error (in `getDataFromREST`?) and could we see the potentially error-throwing code part? Also, you say you `check if entity = "NULL"` but using `=` is not a comparison, but an assignment. Please clarify. – dirkk Dec 03 '15 at 11:10
  • As above.. entity is an array of numbers which is declared and fetched in the service. I edited my code to compare and not assign `"NULL"`. My thoughts were that if an array of promises were not returned to `q.all` why is that not considered an error callback? But that's besides the point, because if an array of promises isn't returned to `q.all`, nothing within `apply` function is executed, not even the boolean which is assigned before it. – notAChance Dec 03 '15 at 11:13

1 Answers1

0

Pay attention that the Promise library wraps all errors thrown in enclosed brackets code, so you are really recommended to test your functions before wrapping them in a promise mechanism.

In your case:

keepICorrect: function (security) {

        var promises = [];
            for (var i = 0 ; i < entity.length; i++) {
                promises.push(this.getDataFromREST(security, i));
            }
         return promises;
    },

you say that it is a closure, so I figure that this.getDataFromREST should be undefined. Please try to add correct code progressively in order to do not have your errors hidden and remove the error in your code.

morels
  • 2,095
  • 17
  • 24
  • Not sure what you mean, are you implying I am hiding incorrect code? `this.getDataFromREST` is not undefined, the values are passed correctly. – notAChance Oct 24 '16 at 11:01
  • I mean that I run your situation a couple times. In both times I could not read any error on my console because the Promise library wraps any runtime error in a sort of `catchAll {}` logic. About the error I supposed to be: in code you posted it seemed it would be better to call `getDataFromREST` instead of `this.getDataFromREST`. – morels Oct 24 '16 at 14:04