I built a function that creates warning notice using Toastr. (writing in JS, not using Angular).
It asks whether you want to delete a file.
if user presses "YES" button, I want to return boolean value: "True"
if user presses "NO" button, I want to return boolean value: "False"
Problem:
I didn't get a boolean value back, when I called the function fileAlert.
The calling function:
async function noticeUser() {
let promise = new Promise(() => {
fileAlert();
});
let result = await promise;
console.log("The result was: "+result); //after execution: result is empty
};
The function fileAlert() :
<script>
function fileAlert() {
var userAnswer = toastr.warning('Are you sure you want to delete the file?<br /><br /><button type="button" id="yesDelete" class="btn clear">Yes</button> <button type="button" id="noDelete" class="btn clear">No</button>', "Warning");
var userSaid = true;
toastr.options = {
"closeButton": false,
"allowHtml": true,
onShown: function (toast) {
$('#yesDelete').click(function () {
console.log('user clicked yes - he wants to delete the file')
toastr.clear(userAnswer);
return userSaid;
});
$('#noDelete').click(function () {
console.log('user clicked no - he doesnt want to delete the file')
//Deleting warning message from screen.
toastr.clear(userAnswer);
//Returning False so that deletion procedure will not occur
userSaid= false;
return userSaid;
});
},
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-top-right",
"preventDuplicates": false,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": 0,
"extendedTimeOut": 0,
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut",
"tapToDismiss": false
}
};
</script>
My solution has 2 steps:
1) I deleted onShown inside toastr.options and I called onClick functions after the definition of toastr.options.
2) I changed function noticeUser to be:
fileAlert().then(result => {
console.log(result);
if (result == true) {.....} });
fileAlert function after fixing is:
function fileAlert() {
return new Promise((resolve,reject)=>{
var userAnswer = toastr.warning('Are you sure you want to delete the file?<br /><br /><button type="button" id="yesDelete" class="btn clear">Yes</button> <button type="button" id="noDelete" class="btn clear">No</button>', "Warning");
toastr.options = {
"closeButton": false,
"allowHtml": true,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-top-right",
"preventDuplicates": false,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": 0,
"extendedTimeOut": 0,
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut",
"tapToDismiss": false
}
$('#yesDelete').click(function () {
console.log('user clicked yes - he wants to delete the file')
toastr.clear(userAnswer);
resolve(true);
});
$('#noDelete').click(function () {
console.log('user clicked no - he doesnt want to delete the file')
//Deleting warning message from screen.
toastr.clear(userAnswer);
//Returning False so that deletion procedure will not occur
resolve(false);
});
});
};