-2

How do I make a function to continue only after another function which was called by it finished. Something like this :

function FunctionOne() {
  //Do something ...
  FunctionTwo()
  //Rest of the code after two is finished
}

function FunctionTwo() {
  //Some code
}

EDIT:

The exact functions are like this :

function FunctionOne() {
  //Do something ...
  var result;

  for (var i = 0 , i < 100 , ++i){
     result = FunctionTwo()
  }
  //Rest of the code after two is finished
  console.dir(result); //this here gives me undefined
}

function FunctionTwo() {
    $.get("url", function(data)
    {
       result = data;
       console.dir(result); //this gives me the data but the first function is still running and it finishes faster then the data is retured

       return result;
    }
banzsolt
  • 27
  • 1
  • 1
  • 5
  • 6
    As long as `FunctionTwo` doesn't have anything asynchronous, the code will execute the way you want, the way you have it... – Ian Jul 24 '14 at 13:47
  • Yes, this is how Javascript works. Have you tried it? Do you have a specific problem you are trying to solve? Or maybe you are doing something async? Then this might be a duplicate for the "return value from function with AJAX call" line of questions, we already have more than we can close as a duplicate :). – kapa Jul 24 '14 at 13:48
  • yes it has a async in it, I am requesting for some data from an api, and I am requesting it while using the 2nd function,and I want to return it with a function. the only problem is that I need to send more request but I need to wait till I get the result back from the 2nd function (I have the 2nd function in a for loop) – banzsolt Jul 24 '14 at 14:06
  • 3
    @banzsolt Then please be clear next time you are asking a question. Closed as a dupe. – kapa Jul 24 '14 at 14:09
  • @banzsolt Set async to false in your ajax call then, there is no reason to run it async if you then want to wait for it. – Wobbles Jul 24 '14 at 14:17

2 Answers2

1

You have to use jQuery.ajax and set async: false

a simple example:

result = '';

function FunctionTwo() {
    jQuery.ajax({
                 url:    'url',
                 success: function(data) {
                              result = data;
                              console.dir(result); 
                          },
                 async:   false
    });

    alert(result);
}
Filo
  • 2,829
  • 1
  • 21
  • 36
-1

This should already do as you request. Unless you specify FunctioTwo to run asynchronously it will block for you.

Edit in response to your edit: If you want the Ajax call to block then there is no reason to have it as an async call, if you do wish it to remain async then utilize the callback of the ajax call to execute you data handling function instead of placing that code inside the main block.

function FunctionOne() {
  //Do something ...
  var result;

  for (var i = 0 , i < 100 , ++i){
     result = AJAXCall(i)
  }

}
function HandleAJAXResponse(response,id)
{
    console.dir(response)
}

function AJAXCall(id) {
    $.get("url",function(data,id){
        HandleAJAXResponse(data,id);
    });
}
Wobbles
  • 3,033
  • 1
  • 25
  • 51
  • How do you specify a function to run asynchronously? – kapa Jul 24 '14 at 13:54
  • The easiest way is to trigger it by a timer set for 0. – Wobbles Jul 24 '14 at 13:57
  • But then you are not running it. You are running the `setTimeout` function and only pass your function as a parameter. – kapa Jul 24 '14 at 14:00
  • 1
    @kapa `setTimeout(myFunction, 0);` – Rory McCrossan Jul 24 '14 at 14:00
  • @kapa events run async, so any function you set in setTimeout will run async, the only other method is callbacks and that is a pain – Wobbles Jul 24 '14 at 14:01
  • 1
    @Wobbles My argument is that you cannot call/run a function asynchronously. You can specify callbacks that will be called/run later by other code (event handlers, `setTimeout`, etc.). – kapa Jul 24 '14 at 14:07
  • @kapa events do in fact run asynchronously, any function specified by creating a timeout 0 will execute immediately and not block code after. – Wobbles Jul 24 '14 at 14:12
  • 1
    @Wobbles Either I don't understand what you're trying to say, or a Javascript function cannot run immediately (sync) and not block code after (async) at the same time. – kapa Jul 24 '14 at 14:18
  • @kapa You can run async, meaning executing two functions at once, they just are not multi threaded and will only process one block then look for other async functions waiting to process a block then bounce back and forth. Because of this behavior you can create two timers with callbacks that are never ending loops and both will loop at the same time bouncing back and forth between each other, this is how sites are able to do live AJAX aswell – Wobbles Jul 24 '14 at 14:21
  • @Wobbles No problem, we are not talking about the same thing it seems. – kapa Jul 24 '14 at 14:27