I have been experimenting with returning synchronous results from an aura:method called by a parent component. I have observed two use cases in which results are never returned:

  1. When the aura:method is called from a function that is invoked from an event handler
  2. When the aura:method is called from $A.getCallback()

Does this mean that aura:method can only return "synchronous" results in the context of the framework lifecycle - i.e. the results will not be available to a calling function invoked in an asynchronous context?

This makes sense on a certain level, but I'm struggling with the "why" behind this.

  • I am at a loss as to how to return a synchronous result, as every use case I test returns 'undefined' for the var that references the method providing the return. – Mike Topalovich Feb 14 '18 at 13:16
  • Can you give a code example? From what I understand of your question, I've used scenario 1 successfully, so there may be some subtlety that's not obvious without the code. – Aidan Feb 14 '18 at 14:12
  • I will work on abstracting a code sample. What I'm building right now would be too complex to follow without a ton of annotation. I've narrowed my hypotheses down do this either being a limitation in the data types that can be returned (seems OK with String when using a simple controller action, but Object or Object[] always returns undefined), or an issue with the return for the method occurring in a function call in the Helper down the stack. Unfortunately I had to give up and go back to using an event-driven model. – Mike Topalovich Feb 14 '18 at 15:56

Both of this use cases , calls for asynchronous action or a callback .The docs hint at this a little

Asynchronous code can continue to execute after it returns. You can’t use the return statement to return the result of an asynchronous call because the aura:method returns before the asynchronous code completes. For asynchronous code, use a callback instead of a return statement.

Hence what i think is you just need to use callback pattern documented here

Essentially pass a function as attribute and set the callback .

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.