6

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?

3

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>
| improve this answer | |
  • Thought passing a callback function was usually necessary when the response is only available later? – Keith C Jul 12 '17 at 17:32
  • Typically, but in this case, since there is no server trip or setTimeout the code runs straight through. – wellmstein Jul 12 '17 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.