You need to sign in to do that
Don't have an account?
Adam Tolbert
Repeated Apex calls from a helper.Js
Can someone please explain to me why the following code doesnt execute on its own?? It will only run one time and then requires a touch interaction on the screen to continue to run. Code works as expected if you rapidly click the screen, but hangs itself if the user doesnt click the page.
testFunction : function(component, token, tempSo) { var action = component.get("c.forLoopTest"); action.setParams({ "I": 5 }) console.log("I ran") action.setCallback(this, function(res) { console.log("Callback ran") console.log(res.getReturnValue()) if(res.getReturnValue() < 20) { setTimeout(() => { console.log(res.getReturnValue()); action.setParams({ "I": (res.getReturnValue() + 1) }) $A.enqueueAction(action) }, 500) } else { console.log("You did it!") } }) $A.enqueueAction(action) },
Apex Controller class @AuraEnabled public static Integer forLoopTest(Integer I){ Integer newI = I; return newI; }
Refer this link
https://rajvakati.com/2018/05/29/using-promise-in-lightning-component/
See main article here (https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IIF3QAO)
This was more of an abstraction of the above article wondering if there is an easwier way of queueing tasks within walesforce on a time delay in order to not overload our API daily budget with unecessary API calls as that can become quite expensive even if you were to have a smaller group of sales people, if it is making over 5 API calls a second and the response takes 15-20 seconds to return a positive result, with a salesforce fo 100 people using this feature 5 times a day, best case scenario, this one API call could be using 50,000 calls a day which can not only use a large portion of a daily limit, but can blow right through it in some cases.
Again, I can write seperate promises until the end of time but the main issue here is the setTimeout not firing the Apex action. Any further assistance would be greatly appreciated!
[https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_cb_mod_ext_js.htm]
And this little gem is exactly waht I was looking for! The issue I was having was indeed a event loop / Framework Lifecycle / callstack problem. I was actually able to prove that my code was indeed queueing the events into their stack but never executing them. This is because if you do something like a window.setTimeout(() => {logic}, 5000) that gets put onto another callstack outside of the main stack and the framework has no idea that it needs to execute it.
This $A.getcallback() allows the component to pull that event back into its own callstack and execute after the delay. Hoping this can save someone the time I lost here!