0
$scope.savekbentry = function (value) {
        console.log('save clicked');
        console.log(value);
        console.log($scope.kbentry.kbname);

        $scope.kbentry.mode = value;
        var kbname = $scope.kbentry.kbname;
        var kbdescription = $scope.kbentry.kbname;
        var kbmode = "";
        var type = "";
        if ($scope.kbentry.mode == 'symptom') { kbmode = 1; type = 'SYM' }
        if ($scope.kbentry.mode == 'allergy') { kbmode = 3; type = 'ALG' }

        $http.post('../AjaxRequestData.aspx/AddKBEntry', { KB_Name: kbname, KB_Des: kbdescription, KB_Mode: kbmode })
        .success(function (data, status, headers, config) {

            $scope.transport = {
                method: 'post',
                read: '../WM_Autocomplete/GetAutocompleteData.aspx/GetSingletonLst',
                params: { type: type }
            }
        })
        .error(function (data, status, headers, config) {

        });

        clear();
    }

In the above code I wanted to call another asynchronous post method after success of first post. Currently its not working as per above code. How to handle this by call back functions?

2
  • 2
    where is the second post in above code ? Commented Nov 1, 2016 at 8:47
  • Check after success of first post Commented Nov 1, 2016 at 8:48

1 Answer 1

1

You can use then() to chain and resolve $http request promises, eg:

var getSingletonPromise = function(type){
  return $http.post('.../GetSingletonLst', type) // returns a promise 
};

var addEntryPromise = function(params){
  return $http.post('.../AddKBEntry', params)  // returns a promise
};

$scope.savekbentry = function (value) {

  addEntryPromise().then(function(){ // use then() to resolve your promise
    // addEntry onSuccess
  }).getSingletonPromise().then(function(){
      // addEntry onSuccess
    }); 
}

You might also consider seperating the $http requests into a seperate factory/service and adding error handeling.
Note that the above code has not been tested but provides an outline of a possible solution.

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.