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
Flint LockwoodFlint Lockwood 

Function Order of Operation in Lightning

I am passing the return value from one function in another function in Lightning (sample below). However, the return value from the first function is always undefined. 
 
myfunction1: function(component)
{
      //call an Apex method
     //return result of apex method
}

myfunction2: function(component)
{
     var result = this.myfunction1(component);
     console.log(result) //RESULT IS ALWAYS UNDEFINED
}

 
Best Answer chosen by Flint Lockwood
Akhil AnilAkhil Anil
Hi Flint,

The lightning framework works in an asynchronous fashion. So, when you call the function1 method from function2, it will just enqueue it and not actually execute it at that point. They are just churned out into multiple threads. You will have to put the logic you intend to put in function2 within the callback function of function1.

myfunction1: function(component)
{
      //call an Apex method
     //return result of apex method
     //function2 logic should be executed here in the CallBack method here.
}


myfunction2: function(component)
{
     var result = this.myfunction1(component);
     console.log(result) //RESULT IS ALWAYS UNDEFINED
}

That should work !

All Answers

mandeep singh 42mandeep singh 42
You can save returm value from apex method in a variable in myfunction1 and pass that variable to myfunction2.

myfunction1: function(component)
{
      //call an Apex method
     //return result of apex method
     var result = {return result of apex method}
     myfunction2(result)
}

myfunction2: function(component,result)
{
     //var result = this.myfunction1(component);
     console.log(result) //RESULT IS ALWAYS UNDEFINED
}
Flint LockwoodFlint Lockwood
I tried that as well. The result is always undefined. I am not sure why. 
Akhil AnilAkhil Anil
Hi Flint,

The lightning framework works in an asynchronous fashion. So, when you call the function1 method from function2, it will just enqueue it and not actually execute it at that point. They are just churned out into multiple threads. You will have to put the logic you intend to put in function2 within the callback function of function1.

myfunction1: function(component)
{
      //call an Apex method
     //return result of apex method
     //function2 logic should be executed here in the CallBack method here.
}


myfunction2: function(component)
{
     var result = this.myfunction1(component);
     console.log(result) //RESULT IS ALWAYS UNDEFINED
}

That should work !
This was selected as the best answer
Flint LockwoodFlint Lockwood
Thanks. That did the trick