[SalesForce] Is a synchronous method call possible in Lightning Components

A Lightning Component – lets call it component B – can define a method (that can have arguments) like this:

<aura:method name="doAbc" action="{!c.doAbc}"/>

that can be called from another component – lets call it component A – quite simply in the controller or helper JavaScript:

cmpB.doAbc();

But this is implemented inside the framework via an event:

var eventDef = $A.$eventService$.$getEventDef$("aura:methodCall");

and so is asynchronous. The JavaScript code immediately after the call can't rely on the side effects of the call and also there is no way such a method call can have a return value.

Is there a simple pattern that can be used to achieve (or simulate) a synchronous call between two components?

Best Answer

I believe those calls are synchronous. I have code like this:

var resultOfB;
cmpB.doAbc( param1, function(returnVal){ 
    resultOfB = returnVal; 
} );

if(resultOfB){ ...}

Your method def:

<aura:method name="doABC" action="{!c.doNext}" 
  description=""> 
    <aura:attribute name="param" type="Object" /> 
    <aura:attribute name="callback" type="Object" /> 
</aura:method>