-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');
    });
}
8
  • Simple solution : execute the first funciton, and at the end of it, call the second one. More complicated solution : Promise.all() Commented Jan 8, 2018 at 10:23
  • docs.angularjs.org/api/ng/service/$q plz check this Commented Jan 8, 2018 at 10:23
  • If i execute first and at end call the second, sometime second one working first Commented Jan 8, 2018 at 10:24
  • 1
    what does updateStartJobManual return? Commented Jan 8, 2018 at 10:24
  • 2
    if any of those functions are asynchronous, then, no to your title, you can not make it work synchronously Commented Jan 8, 2018 at 10:25

1 Answer 1

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');        
};
Sign up to request clarification or add additional context in comments.

2 Comments

i Tried this not working correctly, i am beginner to JS, will you post some answers with my code located in question.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.