Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax chained call complete

Tags:

jquery

ajax

I have three functions. I want to do something when function two (after one) and three are all done. What should I do?

function one(){
    $.ajax({
      url:'url'
    }).done(function(res){
      two();
    })
}


function two(){
    $.ajax({
      url:'url'
    })
}

function three(){
    $.ajax({
      url:'url'
    })
}

enter image description here

like image 867
Alexander Avatar asked Feb 20 '26 02:02

Alexander


1 Answers

Return the $.ajax promises from each function then you can use $.when()

function one() {
  return $.ajax({
    url: 'url'
  }).then(two);      
}

function two() {
  // return request promise
  return $.ajax({
    url: 'url'
  })
}

function three() {
  // return request promise
  return $.ajax({
    url: 'url'
  })
}

$.when(one(),three()).then(function(){
   //all three requests completed here
})
like image 116
charlietfl Avatar answered Feb 23 '26 15:02

charlietfl