0

My question is, how can I call a function after another function containing ajax is complete?

I don't want to use a delay. I used the jquery done() function, but it is maint only for ajax:

$.ajax().done([//second function])

The thing is that I have the ajax stored in a function, and the done methode cannot work this way: ajaxFunction().done(function(){});

Is there any other solution?

  • 1
    Can you post your `ajaxFunction` code ? You should be able to do that using callback but if we have the code of our `ajaxFunction` it will be easier to show you how to do. – Dash Apr 21 '17 at 10:37
  • Yes it can work that way, `jQuery.ajax` returns a promise, just return that promise from `ajaxFunction`. – Useless Code Apr 21 '17 at 10:37
  • 1
    Try this link http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call It has a detailed solution to your problem. – Saurabh Verma Apr 21 '17 at 10:43

2 Answers2

0

Maybe try :

ajaxFunction().then(function(){/*your code here*/})
abraham63
  • 443
  • 4
  • 17
-1
function function1() {
    $.ajax({
        url : 'serverUrl',
        type: 'GET',
        success : function2
    })
}

function function2([dataFromFirstAjax-Optional]){
    //do some stuff
}
Elie Nassif
  • 464
  • 4
  • 11