function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhinav Sharma.Abhinav Sharma. 

call multiple methods in client side controller synchronously

Hi

I want to call multiple methods in client side controller synchronously i.e. in a particular sequence so that I can use the response from one in second one and so on.

How can I achieve this in lightning client side components?

Regards,
Abhinav Sharma
James LoghryJames Loghry
Utilize call backs 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);
    }
}