jquery how to wait for ajax call finishes to return code example
Example 1: jquery wait for all ajax requests to complete
$.when(ajaxCall1(), ajaxCall2()).done(function(ajax1Results,ajax2Results){
});
function ajaxCall1() {
return $.ajax({
url: "some_url.php",
success: function(result){
console.log(result);
}
});
}
function ajaxCall2() {
return $.ajax({
url: "some_url.php",
success: function(result){
console.log(result);
}
});
}
Example 2: wait for ajax to finish
function functABC() {
return new Promise(function(resolve, reject) {
$.ajax({
url: 'myPage.php',
data: {id: id},
success: function(data) {
resolve(data)
},
error: function(err) {
reject(err)
}
});
});
}
functABC().then(function(data) {
console.log(data)
}).catch(function(err) {
console.log(err)
})