wait variabl until not getting function response jquery code example
Example 1: 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)
})
Example 2: how to wait till jquery post request has been made
$.when(ajax1(), ajax2(), ajax3(), ajax4()).done(function(a1, a2, a3, a4){
});
function ajax1() {
return $.ajax({
url: "someUrl",
dataType: "json",
data: yourJsonData,
...
});
}