You need to sign in to do that
Don't have an account?
Loooking for Best Practice : Chaining two asynchronous Apex calls from a Lightning Component
In my company I was facing a scenario where I had to chain two Apex asynchronous calls:
First I needed to call myApexMethod1 which returns a result myResult
Secondly I needed to call myApexMethod2 but needed to provide myResult as a parameter to the method.
So this is the first version I coded :
This code compiles well but there is a problem:
Since the calls are asynchronous, by the time we reach :
action2.setParams({ myParam : myResult });
it is not guaranteed that myResult is filled, because at that time nothing guarantees that myApexMethod1 has executed since it is asynchronous.
To avoid this behaviour, I decided to nest the second call inside the callback of the first method.
That gives the following code :
I have tested it and it works.
But my question is:
Is it the proper way to chain these asynchronous calls?
Next time maybe I will have to chain a lot of asynchronous calls and this pattern does not look very clean?
Is there a better practice to chain these asynchronous calls?
First I needed to call myApexMethod1 which returns a result myResult
Secondly I needed to call myApexMethod2 but needed to provide myResult as a parameter to the method.
So this is the first version I coded :
({ "myFunction" : function(cmp) { var action = cmp.get("c.myApexMethod1"); action.setCallback(this, function(response) { var state = response.getState(); if (state === "SUCCESS") { myResult = response.getReturnValue(); } }); $A.enqueueAction(action); var action2 = cmp.get("c.myApexMethod2"); action2.setParams({ myParam : myResult }); action2.setCallback(this, function(response) { var state2 = response.getState(); if (state2 === "SUCCESS") { //Do something } }); $A.enqueueAction(action); } })
This code compiles well but there is a problem:
Since the calls are asynchronous, by the time we reach :
action2.setParams({ myParam : myResult });
it is not guaranteed that myResult is filled, because at that time nothing guarantees that myApexMethod1 has executed since it is asynchronous.
To avoid this behaviour, I decided to nest the second call inside the callback of the first method.
That gives the following code :
({ "myFunction" : function(cmp) { var action = cmp.get("c.myApexMethod1"); action.setCallback(this, function(response) { var state = response.getState(); if (state === "SUCCESS") { myResult = response.getReturnValue(); var action2 = cmp.get("c.myApexMethod2"); action2.setParams({ myParam : myResult }); action2.setCallback(this, function(response) { var state2 = response.getState(); if (state2 === "SUCCESS") { //Do something } }); $A.enqueueAction(action); } }); $A.enqueueAction(action); } })
I have tested it and it works.
But my question is:
Is it the proper way to chain these asynchronous calls?
Next time maybe I will have to chain a lot of asynchronous calls and this pattern does not look very clean?
Is there a better practice to chain these asynchronous calls?
Shouldn't line #20 be $A.enqueueAction(action2); instead of $A.enqueueAction(action); ?
I'm no lightning expert but that matches the async js patterns i've seen in the wild.
For cleanliness, i think you could have 2 functions and as long as you call function2 like you are doing in the Success processing block.
At that point, it might be more proper to have the 2nd function set as a helper but i'm still figuring that controller/helper distinction out myself.
</imho>
;