0

Inside an HTTP call, I am checking for the response

   promise.then(function(res){
        var value = null;
        var keepGoing = true;
        for(var key in res) {
            if(keepGoing){
                //If the response object has a null value for any of the given keys, we assume the API has failed
                if(value === null)
                    keepGoing = false;
            }
        }
        //And we reject the promise
        if(!keepGoing){
            $q.reject("Bad data");
        }

        //Else, we loop through the keys
        if(res.length != 0){
            var keysArray = Object.keys(res);   
            ....
            ....

    },function(error){
        vm.continuing = false;
        vm.errorInContinue = true;
    });

What I want that whenever I reject that promise, do not execute the next lines and jump to the error block.

What am I doing wrong?

I tried return $q.reject("Some bad data") also, but did not help.

StrugglingCoder
  • 4,781
  • 16
  • 69
  • 103
  • `return $q.reject("Bad data");` is correct way. But as you're expecting to call current error block, the subsequent `chain promise` function will get called by this way. – Pankaj Parkar Sep 20 '17 at 13:42
  • @PankajParkar I tried but this is not going to `fucntion(error) ...` block. – StrugglingCoder Sep 20 '17 at 14:11
  • Apart from having to use `return`, have a look at the [difference between `.then(…, …)` and `.then(…).catch(…)`](https://stackoverflow.com/q/24662289/1048572) – Bergi Sep 20 '17 at 14:33

0 Answers0