-1

I am trying to do HTTP chaining. If a HTTP status is non 200 then i want to break out of promise chain. But with my current solution it is being executed till end even in case of error or non 200 status.

factory.login=function(username,password){
        var url=String(myconfig.url)+"authentication/token/new";
        var requestToken="";
        var sessionID="";
        var defer=$q.defer();

        $http({
            url:url,
            params:{
                api_key:"my API Key",

            }
        }).then(validateWithLogin,HTTPErrorHandler)
            .then(getSessionID,HTTPErrorHandler)
            .then(function(data){
                if (data.status!=200){
                    defer.reject(data);
                }
            sessionID=data.data.session_id;
            $cookies.put("sessionid",sessionID);
        },HTTPErrorHandler);
        function HTTPErrorHandler(data){
            console.log("HTTP Error happened");
            console.log(data);
            defer.reject("http error");  
        }
        function validateWithLogin(data){
            if (data.status!=200){
                defer.reject(data);
            }
            requestToken=data.data.request_token;
            console.log("validate with login"+data);
            return $http({
       url:String(myconfig.url)+"authentication/token/validate_with_login",
                params:{
                    api_key:"my API Key",
                    username:username,
                    password:password,
                    request_token:requestToken
                }
            })
        }
        function getSessionID(data){
            if (data.status!=200){
                defer.reject(data);
            }
            return $http({
                url:String(myconfig.url)+"authentication/session/new",
                params:{
                    api_key:"my API Key",
                    request_token:requestToken
                }
            })
        }

    };

1 Answer 1

3

Because It's not enough to just reject your promise but you have to return with your deferred object.

I'd suggest to use the sorthand version of it: return $q.reject();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.