-1

This is my function:

$scope.saveManualResendDraft = function(todo) {
    if ($scope.editMode) {
        updateStartJobManual();
        byeSendManualInputDirectly();
    } else {
        console.log('bye');
    }
};

I have two functions updateStartJobManual() and byeSendManualInputDirectly().

I want to complete first function fully then i need to move to second, How to do? is it possible to do using promises? I need some piece of code.

function byeSendManualInputDirectly() {
    if ($window.confirm("Do you want to send this messages?"))
        addProfSms();
    else
        console.log('no');
}

function addProfSms() {
    $http.post('/api/sendprofsms', $scope.draft).then(function(response) {
        swal("Good job!", "Message sended!", "success")
        //  $state.reload();
    });
}

function updateStartJobManual() {
    $http({
        method: 'POST',
        url: '/api/updatestartjobmanual',
        data: $scope.draft
    }).then(function(response) {
        $scope.currentItem = response.data;
        $scope.todos[$scope.currentItemIndex] = response.data;
        $scope.editMode = false;
        console.log('draft:', response.data);
        $state.reload();
        // toastr.success('Updated Successfully');
    }, function(response) {
        console.log('error');
    });
}
Rex Low
  • 2,069
  • 2
  • 18
  • 47
Mohamed Sameer
  • 2,998
  • 3
  • 22
  • 51

1 Answers1

1

Your actual code already executes updateStartJobManual and byeSendManualInputDirectly synchronously.

However, if your functions are handling promises, both will end prematurely with a background job running. So, let's chain the promises to perform one after another.

Assume your byeSendManualInputDirectly (and byeSendManualInputDirectly) is made as such:

function byeSendManualInputDirectly(){
   return $http.post('myApiAddress', {myParam: true});
}

This way, the function is returning a promise.

To concatenate updateStartJobManual and byeSendManualInputDirectly you can simply:

updateStartJobManual().then(function(){
   byeSendManualInputDirectly()
});

I suggest you to read some articles about promises and to understand how they works (see this documentation about usage of $q, the library for promises angularjs uses)

Edit based on OP's code:

simply add a return to your function updateStartJobManual, this way:

function updateStartJobManual() {
    return $http({
        method: 'POST',
        ...
}

and in your saveManualResendDraft, add a then() to handle the promise:

$scope.saveManualResendDraft = function(todo) {
    if ($scope.editMode) 
        updateStartJobManual().then(byeSendManualInputDirectly);
     else 
        console.log('bye');        
};
illeb
  • 2,942
  • 1
  • 21
  • 35
  • i Tried this not working correctly, i am beginner to JS, will you post some answers with my code located in question. – Mohamed Sameer Jan 08 '18 at 10:34
  • i tried working good, but this hitting two time " function addProfSms() { $http.post('/api/sendprofsms', $scope.draft).then(function(response) see my quesion i have this function – Mohamed Sameer Jan 08 '18 at 10:47