Normally an asynchronous function, let's say async(arg, cb)
, provides a callback function placeholder (cb
) at the end so you can run your own code when the asynchronous routine ends. What if the asynchronous function has no callback argument? Clearly with cb
missing in:
async(args);
myFunction();
myFunction()
will run first. How can I make myFunction()
run only after async
ends? I tried writing my own callback wrappers without success; they were merely redirecting the function calls.
EDIT I'm interested in an answer to the above but I will give an example of how I ran into this situation.
My example is in the Google Maps Directions service. The asynchronous function is a drawing routine called directionsDisplay.setDirections(result);
, and I want to recenter and zoom in after the drawing completes. I put together a setTimeout
of 3 seconds so that what I want happens, but it's a clumsy solution. Altering the Google Maps API seems out of the question. What could be better?
The directions service is here.
Simply choose a city in either dropdown. After 3 seconds the map should move.