1

I am still learning promises in angular and have this bit of code where I am making a "GET" request two times. I want to run one get request before calling the other. This is working fine, but how would I handle errors here? If I get an error for my first GET request how do I find out what that error is and prevent my code from calling the second GET request? Examples with my code would be most helpful.

apiServices.login = function(user,password,callback) {
$http.get("http://magainteractive.com/prototypes/cisco-ima-dashboard/cms/web/api/login/login/?username="+user+"&password="+password+"")
        .then(function(contentResponse){
            resultsObject.content = contentResponse; 
            return $http.get("http://magainteractive.com/prototypes/cisco-ima-dashboard/cms/web/api/data/list/"); 
        })
        .then(function(dataResponse){
            resultsObject.reports = dataResponse;
            resultsObject.success = 1;
            console.log(resultsObject);

            callback(resultsObject);
            apiServices.useData(resultsObject);
        }); 
}

dummyData.login(username, password, function (dataStatus) {

            if (dataStatus.success = 1) {

                $rootScope.loggedIn = true;
                $rootScope.selectedDashboard = 1; 
            } else {
                console.log("Error");
            }
        });

2 Answers 2

1

I would do things slightly different from Lucas, I prefer chaining a catch block( basically it would act like the synchrounous try...catch block we use) rather than adding an error callback function so code would be like:

return $http.get(url1)
  .then(function(result){
    resultsObject.url1 = result;
    return $http.get(url2);
  }).then(function(result){
    resultsObject.url2 = result;
    return resultsObject;
  }).catch(function(error){
    // handle error.
  });

P.S: most of your code is fine, but I am not really sure why you have that callback(resultsObject);, when you are using promises, callbacks are redundant, you could just return the promise chain $http.get...

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

2 Comments

I work this way too, I think it's pretty neat to chain a catch at the end! I totally agree with you.
@mido thanks! I edited my answer to show you why I am using callback. I use it to bring the resultsObject into my login controller as "dataStatus". This will check if my login credentials are true.
0

You can pass a second parameter in the first callback handling. This will trigger if there's an error in the request, then you can handle it however you want:

 $http({
   method: 'GET',
   url: '/someUrl'
 }).then(function successCallback(response) {
     // this callback will be called asynchronously
     // when the response is available
   }, function errorCallback(response) {
     // called asynchronously if an error occurs     
     // or server returns response with an error status.
   });

Or in your coding:

$http.get('/someUrl').then(successCallback, errorCallback);

More information here

Your code would look like:

$http.get("http://magainteractive.com/prototypes/cisco-ima-dashboard/cms/web/api/login/login/?username="+user+"&password="+password+"")
    .then(function(contentResponse){
        resultsObject.content = contentResponse; 
        return $http.get("http://magainteractive.com/prototypes/cisco-ima-dashboard/cms/web/api/data/list/"); 
    }, function(error){ 
             //HANDLE ERROR HERE
       })
    .then(function(dataResponse){
        resultsObject.reports = dataResponse;
        resultsObject.success = 1;
        console.log(resultsObject);

        callback(resultsObject);
        apiServices.useData(resultsObject);
    }); 

2 Comments

this looks good but what if I wanted error handling for both http requests? Would I add another function(error) after the second then function?
Exactly. In my example, the last line would change from }); to: }, function (error) { //asd });

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.