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 

Calling same Apex class API req from Lightning repeatedly while if(x='foo') else {<res.getReturnValue()>}

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
Use Promise  try like below 

Refer this  links

https://rajvakati.com/2018/05/29/using-promise-in-lightning-component/
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_promises.htm
http://bobbuzzard.blogspot.com/2016/12/javascript-promises-in-lightning_30.html
 
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")

var seventhAction1 = component.get("c.APICall");
 seventhAction1.setParams({
 "body": GetSalesOrderBody,
 "task": "GetSalesOrderDetails"
})
 
 seventhAction1.setCallback(this, function(res1) {
$A.enqueueAction(seventhAction1);

 });
 
 
} else {
console.log(res.getReturnValue())
console.log('Congrats! This API has finished processing and has returned a new order id
of ' + res.getReturnValue().Id + '');
}
});
$A.enqueueAction(seventhAction);

 
Adam TolbertAdam Tolbert
@Raj Thanks for your many responses to my conundrum however none of them have really addressed the two trickier parts of the code. I could just write 30+ identical functions and have them promisified or calling back to the next one but:

a) That doesnt solve the main issue that I need to delay the function call as it can take up to 20 seconds for the third party API to finish processing and I need to delay the subsequent call as to not call out to an API what can be 4 times per second for 20 seconds. I am working to build out this functionality for a larger company and this can not only put unnecessary stress on our Salesforce API daily limits as well as out third party limitations. I have structured the rest of my code as suggested above to complete actions and it all works fine but the problem arises when I need to call the same function, with a delay, until I reach some state of completion on the third party side.

and

b) As I have no idea how long the call will take to process and return a valid response, I would then have to create many identical functions all of which would need to evaluate the response the same way and that seems like a really inefficient way to get the job done, there has to be some way to call the same function from within itself and have it evaluate its response on its own in order to decide if it should resolve and or reject no?

Im thinking that the issue has something to do with the way Salesforce queues and calls its actions but I am still unable to figure out how to delay these calls to a reasonable interval and call itself once that timer has finished 
Alain CabonAlain Cabon
The problem was solved with your new question.