I'm aware that this can be achieved via a Promise but I am struggling to figure out how.
jQuery('.parentDiv').on('click', '.link', function(e) {
jQuery.when(jQuery('.saveBtn').trigger('click', { 'evtData': 'link' })).then(function {
// rest of the logic that should be performed on click of .link
});
});
The click of .saveBtn
calls a function named doAjax
:
jQuery('.saveBtn').on('click', function() {
doAjax()
});
function doAjax() {
var ajaxCall = jQuery.ajax(ajaxObject);
ajaxCall.done(function(data, status, xhr) {
//some logic go here
});
return ajaxCall;
}
Despite this the logic inside the .then
handler is getting executed first i.e before doAjax
completes.
I believe I need to change the jQuery.when(jQuery('.saveBtn').trigger('click',{'evtData':'link'}))
as it may not be getting the Promise state which it should and immediately getting marked as Resolved thereby executing the callback without waiting?.
I tried return doAjax in .saveBtn but that also did not make a difference.
Ideas please.