I have 2 functions related to each other .. _confirm and _process. _confirm is responsible for returning true/false values to _process and based on response _process function will act and process the bid.
BUT THE ISSUE IS:
that the within _process the _confirm is returning undefined rather showing boolean, because it's moving on before data from _confirm
NOTE* using jquery-confirm-3 library for alert/confirm
<script type="text/javascript">
/* Extra confirmation message on place bid */
function _confirm( formname, id_Bid ) {
var response = '';
/* Get bid value, format value and then add to confirm message */
var bidval = jQuery(id_Bid).val();
var bidval = parseFloat(bidval);
if ( bidval > 0 ) {
var floatbidval = bidval.toFixed(2);
var currencyval = "$";
var finalval = currencyval + floatbidval;
if( formname == 'custom' ) {
var confirm1 = 'Do you really want to bid';
} else if( formname == 'direct' ){
var confirm1 = 'Do you really want to directly place this bid';
}
let confirm_message = confirm1 + ' ' + finalval + ' ?';
$.confirm({
title: false,
content: confirm_message,
onAction: function( btnName ) {
response = btnName;
},
buttons: {
confirm: function () {},
cancel: function () {}
}
});
if( response != 'confirm' ) {
event.preventDefault();
return false;
} else{
return true;
}
}
} /* end of function - _confirm() */
function _process( formname, id_Bid ) {
<?php
$enable_bid_place_warning = get_option('uwa_enable_bid_place_warning');
$placebid_ajax_enable = get_option('woo_ua_auctions_placebid_ajax_enable');
/* using page load */
if( $placebid_ajax_enable == 'no' || $placebid_ajax_enable == '' ) {
if( $enable_bid_place_warning == 'yes' ) { ?>
$data = _confirm( formname, id_Bid ); <?php
}
} elseif ( $placebid_ajax_enable == 'yes' ) { /* using ajax */
if( $enable_bid_place_warning == 'yes' ) { ?>
retval = _confirm( formname, id_Bid );
if( retval == true ) {
place_ajax_process( formname );
} <?php
} else { ?>
place_ajax_process( formname );
<?php
}
}
?>
} /* end of function */
</script>
Later researching here, I found a couple of answers to use .Deferred() and return a promise .. but the promise is also returning unidentified
later I made the following changes but still no luck
function _showConfirmDialog( message ) {
let def = $.Deferred();
$.confirm({
title: false,
content: message,
onAction: function( btnName ) {
def.resolve(btnName);
},
buttons: {
confirm: function () {},
cancel: function () {}
}
});
def.promise();
}
/* Extra confirmation message on place bid */
function _confirm( formname, id_Bid ) {
var response = '';
/* Get bid value, format value and then add to confirm message */
var bidval = jQuery(id_Bid).val();
var bidval = parseFloat(bidval);
if ( bidval > 0 ) {
var floatbidval = bidval.toFixed(2);
var currencyval = "$";
var finalval = currencyval + floatbidval;
if( formname == 'custom' ) {
let confirm1 = 'Do you really want to bid';
} else if( formname == 'direct' ){
let confirm1 = 'Do you really want to directly place this bid';
}
let confirm_message = confirm1 + ' ' + finalval + ' ?';
$.when(_showConfirmDialog(confirm_message)).then(function( response ) {
if ( response == 'confirm' ) {
return true;
} else {
return false;
}
});
}
}
i also tried to use async and await but no luck.
You are nearly there with your second attempt but
_showConfirmDialog()needs to return Promise. It currently returnsundefined.You should also consider exploiting the promise's settled state (resolved/rejected) rather than resolving with
btnName. Thus the code is more independent of natural language.You might write:
Similarly,
_confirm()should also return promise in order to keep its caller informed of the dialog's outcome.You might write:
Thus,
_confirm()returns a promise that will resolve if the user clicks "confirm" or rejects if the user clicked "cancel" (rather than resolving wirth true|false).Note: Unless there's a compelling reason to want a jQuery promise, it's better to Promisify
jQuery.confirm()with a native JS Promise.You might write:
Thus,
_confirm()(as above) will also return a native Promise.