I tend to run into this problem quite often, and my solutions never really work the way i wanted to.
Basically, I want one function to wait till the one before is completely finished running.
For example, I have a gps locating function, and i want another function to run once the gps one completes its tasks.
I am not putting any code for this because i would like to see how people do it without seeing my mine, as to be completely objective.
I am using phonegap, jquery-mobile and mkaing hybrid apps.