0

I have this code :

$(document).ready(function(){
    doSomething();
    var a = getSomething();
})

function doSomething(){
    $.ajax(
        //insert something to database here
    )
}

How do I make sure doSomething() has already finished its ajax call before calling getSomething() without using async: false or changing the code inside doSomething() method?

tickwave
  • 3,335
  • 6
  • 41
  • 82
  • write getSomething() method inside complete callback of doSomething() ajax call without this you can't control ajax async calls – Frebin Francis Jan 20 '16 at 05:07
  • @FrebinFrancis That would require changing the code inside `doSomething`... – lc. Jan 20 '16 at 05:08
  • Yes it will work, but do you have another alternative because I can't change the code inside `doSomething()` method like I said in the post. – tickwave Jan 20 '16 at 05:09
  • @warheat1990 without this change we are unable to control the behavior of that ajax call because it's async. – Frebin Francis Jan 20 '16 at 05:10
  • You can't change the code inside `doSomehitng()` even a bit? even adding return ? – Norlihazmey Ghazali Jan 20 '16 at 05:11
  • I think we need more information as to why you can't change `doSomething` or what you are trying to do. Otherwise the only solutions I can think of are very very hack-y. – lc. Jan 20 '16 at 05:11
  • @warheat1990 or you can write ajax stop setup function inside that page for the method see this link https://api.jquery.com/ajaxStop/ – Frebin Francis Jan 20 '16 at 05:12
  • JavaScript cannot wait. Anything that needs to wait for an asynchronous function to finish needs to be initiated from that function's callback (or chained to its promise, or equivalent). The linked article, and the several articles linked from there, give a canonical, comprehensive explanation to the problem as well as solution. In case you really can't change the function, then polling (via `setTimeout`, for example) for any signs of completion is the only way, really. – Amadan Jan 20 '16 at 05:14

0 Answers0