Wednesday, 28 March 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

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...