Q:

Run code after function completes and get returned value

Imagine i have a simple javascript function:
  function someFunction(integer)
  {
    data = integer + 1;
    return data;
  }

I need to call this from inside another function and use the returned value:
  function anotherFunction(integer)
  {
    int_plus_one = someFunction(integer);
    //Do something with the returned data...
    int_plus_two = int_plus_one + 1;
    return int_plus_two;
  }

How can i ensure that the return of anotherFunction return is only returned after someFunction completes? It actually seems to work ok with very fast functions like these. However if someFunction has to do some ajax lookups, the return of aotherFunction fails.
Thanks,
Steve

A:

You do not know when or even if an asynchronous function will complete. The only way to handle this is to use a callback function, a function that gets executed after the async operation has completed.
This was my "aha!" moment: How to return the response from an asynchronous call?

