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
Adam TolbertAdam Tolbert 

Call an Apex Class Recursively from the JS Helper within an if condition

Alright community, Im in a real pickle here and hoping someone can shed some light on what I'm doing wrong, and/or what I can do to work around this particular problem.

I have a Custom lightning component that is calling out to a third party API over multiple synchronous steps and I need to call out to the same API multiple times over a period of 10-30 seconds in order to determine as soon as is reasonable, when that API response changes from Still Processing, to Finished Processing. 

I am currently doing this as follows:
 
var seventhAction = component.get("c.APICall");
seventhAction.setParams({
 "body": GetSalesOrderBody,
 "task": "GetSalesOrderDetails"
})
seventhAction.setCallback(this, function(res) {
 if(res.getReturnValue().includes("InProcess")) {
 console.log("Still Processing Order")
 setTimeout(() => {
    $A.enqueueAction(seventhAction)
 }, 2000)
} else {
console.log(res.getReturnValue())
console.log('Congrats! This API has finished processing and has returned a new order id 
of ' + res.getReturnValue().Id + '');
}

And this function will not recursively call itself... It will just hang there with a console.log() of "Still Processing Order" for up to 5 minutes... However when I click, anywhere on the screen, the task will run as expected. This should not require any user input after it has been fired and I cannot ask my users to just click the screen a ton until the screen goes away. What am i missing here??

Sidenote:
If I run this function without a set timeout, I will execute about 10 times in rapid succession before it hangs itself and awaits click input to get it back on track. I have exhausted my personal supply of potential hacks around/reasons for this problem and am coming up short. Prize goes to the bext answer! 
 
Raj VakatiRaj Vakati
Refer this link 
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IIF3QAO