+ Start a Discussion
B chanduB chandu 

call apex method in js synchronously

Hi my requirement is call controller methods in js synchronously. 
Deepali KulshresthaDeepali Kulshrestha
Hi B chandu,
Please try the given below code with the help of below code you can solve your problem, it may be helpful to you. 

Utilize callbacks to guarantee the first method is finished, before calling the second method, and so on.  Here's an example:
({
    "callAllActions" : function(cmp){
        var action1 = cmp.get("c.action1");
        var action2 = cmp.get("c.action2");
        var action3 = cmp.get("c.action3");

        action1.setCallback(this, function(response) {
            if (state === "SUCCESS") {
                $A.enqueueAction(action2);
           }
        }
       
        action2.setCallback(this, function(response) {
            if (state === "SUCCESS") {
                $A.enqueueAction(action3);
           }
        }

        action3.setCallback(this, function(response) {
            if (state === "SUCCESS") {
               // do something
           }
        }
        $A.enqueueAction(action);
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha