[SalesForce] Call multiple helper methods as order in lightning controller

doInit : function(component, event, helper) {
    helper.method1(component);
    helper.method2(component);
    helper.method3(component);
}

Need to call these helper methods in lightning client side controller synchronously.

Component markup:

Here's my code:

<aura:component controller="EmpAttachmentListController"> 
<aura:handler name="init" value="{!this}" action="{!c.doInit}" /> 
<aura:attribute name="empList1" type="Employee__c[]" /> 
<aura:attribute name="empList2" type="Employee__c[]" /> 
<table>
    <tr>
        <th>Employee Name</th>
        <th>Imgs Count</th> 
    </tr>
    <aura:iteration indexVar="index" var="emp" items="{!v.empList1}"> 
        <tr>
            <td>{!emp.Employee_Name__c}</td>
            <td>
                {!emp.Attachments.le‌​ngth} Imgs <a href="#" onclick="{!c.toggleRecord}" id="{!index}">show/hide</a>
            </td>
        </tr>
        <tr>
            <td>
                <p id="{!'index'+index}" class="showP">
                <aura:iteration var="attach" items="{!emp.Attachments}"> 
                    <img src="{!'/servlet/servlet.FileDownload?file='+attach.Id}" /> 
                </aura:iteration>
                </p>
            </td> 
        </tr>
    </aura:iteration>
</table>
</aura:component>

controller.js

({ doInit : function(component, event, helper) 
    { 
        helper.getEmpList(component); 
    }
});

helper.js

({ 
    getEmpList: function(component) { 
        var that = this; 
        var action = component.get("c.getEmpList"); 
        action.setCallback(that, function(result) { 
            component.set("v.empList", result.getReturnValue());
            that.getEmpList2(component); 
        }); 
        $A.enqueueAction(action);
    }, 
    getEmpList2: function(component) { 
        var that = this; 
        var action = component.get("c.getEmpList2"); 
        action.setCallback(that, function(result) {
            var c = component.find("index0"); ** //Getting undefined error here ** 
            component.set("v.empList2", result.getReturnValue()); 
        }); 
        $A.enqueueAction(action); 
    } 
})

Best Answer

If there's no code in the helper method to invoke apex controller method(@auraenabled), you can call the methods one by one, since they are executed synchronously.

controller.js

doInit:function(component){
    helper.method1(component);
    helper.method2(component);
    helper.method3(component);
}

helper.js

({
    method1:function(cmp){
        // do some stuff
    },
    method2:function(cmp){
        // do some stuff
    },
    method3:function(cmp){
        // do some stuff
    }
})

But if you have to invoke @auraenabled method in the helper method, then you need to chain the methods because @auraenabled callbacks are asynchronous:

controller.js

doInit:function(component){
    helper.method1(component);
}

helper.js

({
    method1:function(cmp){
        var action = cmp.get("c.callingMethod1");
        var self = this;
        action.setCallback(this,function(resp){
            // do some stuff and call method2
            self.method2(cmp);
        });

        $A.enqeueAction(action);
    },
    method2:function(cmp){
        var action = cmp.get("c.callingMethod2");
        var self = this;
        action.setCallback(this,function(resp){
            // do some stuff and call method2
            self.method3(cmp);
        });

        $A.enqeueAction(action);
    },
    method3:function(cmp){
        var action = cmp.get("c.callingMethod3");
        var self = this;
        action.setCallback(this,function(resp){
            // do some stuff
        });

        $A.enqeueAction(action);
    }
})
Related Topic