RSKMR
RSKMR

Reputation: 1892

Angularjs + two http request using $q

I have two http requests. 1. post method 2. get method.

I need to get data from two http request.

I added my code. But i think this is not correct approach. Please suggest me.

Question 1:

After two call to synchronous. Controller

(function() {
    'use strict';
    angular.module('myApp').controller('loginController', loginController);
    loginController.$inject = ['$auth', '$http', '$location', '$scope', '$window', '$rootScope', 'commonService', '$q', '$localStorage'];
    // inject an auth service here!
    function loginController($auth, $http, $location, $scope, $window, $rootScope, commonService, $q, $localStorage) {
        commonService.getHostDetails().then(function(data) {
            commonService.login().then(function(data) {

            }).catch(function(data) {
                alert('Sorry ! test function faild');
            });
        }).catch(function(data) {
            alert('Sorry ! login function faild');
        });
    };

})();

Service Code:

angular.module('myApp').factory('commonService', ['$q', '$timeout', '$http',
function($q, $timeout, $http, commonService) {

    return ( {
        login : login,
        test : test,
    });

    function login() {
        // create a new instance of deferred
        var deferred = $q.defer();
        $http.get('host.json')
        // handle success
        .success(function(data, status) {
            deferred.resolve(data);
        })
        // handle error
        .error(function(data) {
            deferred.reject(data);

        });
        // return promise object
        return deferred.promise;

    }

    function test(formData) {
        // create a new instance of deferred
        var deferred = $q.defer();
        console.log("in service");
        console.log(formData);
        $http.post('/api/test', formData, {
            headers : {
                'Content-Type' : 'application/json'
            }
        })
        // handle success
        .success(function(data) {
            deferred.resolve(data);
        })
        // handle error
        .error(function(data) {
            deferred.reject(data);

        });

        // return promise object
        return deferred.promise;
    }

}]);

Question -2.

Also please suggestme two http request depends on two api. First http request data need to parse into second api.

Upvotes: 0

Views: 1143

Answers (1)

Daniel
Daniel

Reputation: 609

Promises are a great way of chaining you calls.

$http.post("your api").then(function(response) {
  //do something
  return response.data;
}).then(function(data) {

  return $http.get("you other api")
}).then(response_from_second_api) {

});

If you have two calls then need to be resolved before you can do anything you could do something like that

var promise1 = $http.get("");
var promise2 = $http.get("");

$q.all([promise1,promise2]).then(values) {
   var value_of_promise1 = values[0];
   var value_of_promose2 = values[1];
});

Upvotes: 1

Related Questions