0

I'm a newbie to javascript and I'm struggling with the whole callback and asynchronous thing. I have a function called 'RunJob' which executes another function called 'GetData' several times. After these functions have finished executing I wish to do some more processing, but not until the those functions have finished. How can I do this?

//a button with 'RunJob()' as the onclick event

function RunJob() {

    ProcessData(GetData, "condition1");
    ProcessData(GetData, "condition2");
    ProcessData(GetData, "condition3");

    //More processing when the ajax calls in 'GetData' have finished
}

function ProcessData(callback, myVariable) {
    callback(myVariable);
}

function GetData(myVariable) {
    //Multiple ajax calls which will take a while
}
Simon
  • 47
  • 5
  • If we don't know how you're doing your ajax calls, it's difficult to understand what they return. You should have a look at deferred APIs to find solutions. Jquery.Ajax implements such an API that you should have a look at. – Deblaton Jean-Philippe Jan 18 '17 at 16:34
  • Parallel, or one after another? And how are these ajax calls in GetData processed back to the RunJob function? – Jonas Wilms Jan 18 '17 at 16:35
  • Have a look at [`Promise.all`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) – user3297291 Jan 18 '17 at 16:36

0 Answers0