Wednesday, March 28, 2018

Javascript method chaining



Didn't know how else to put this.



Say I have a JavaScript method that makes some AJAX calls:



function makeAJAXCalls()
{
// who knows how long this will take

}


And I don't want the next function to execute until all the AJAX calls are complete. I also don't want to put the next function call into the success callback for the AJAX call.



Is there a way I can like chain these, so the next function doesn't get called until makeAJAXCalls() is done everything it needs to do?


Answer



var queueCount =0;
function makeAjaxCalls()
{

queueCount+=7; // or however many calls you need to make.
// make your ajax calls. onSuccess (or failure), call checkQueue();
}
function checkQueue()
{
queueCount--;
if (queueCount <=0)
{
doNext();
}

}


This leaves the calls asynchronous, which may be the intent here.


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...