In the Swal Github is advised to ask questions to SweetAlert2 in SO, therefore my posting here...
Behind a save button in my (asp.net core) app, I check some things and - in some cases - have to ask the user (yes/no) and - depending on his selection - have to overwrite some data before store.
If I use an (jQuery) alert or a confirm, the code stops, until the user has clicked a button.
In Swal, I don’t have found any way (including “.then(“) to stop the code, until the user has clicked a button.
The message is shown, but the code runs further....
Is it really not possible to stop the code with Swal...?
=> If there is a way, how to do...?
Thanks
It seems, as there is really now way to stop the JS code until the user has done his chosen
So.. I had to change my logic.
Logic before:
To check something ( 1 ) , show an swal (e.g. set focus to a control) and then return in code works (of cause of the return).
This way,the message is showed (until the user close it) and the data are not stored.
To ask the user something in a swal (Do you really want to..." and then do some things based in the answer of the user before the date are stored, don't work, as the JS Code ist not stopped until the user has made a choice.
Der swal is showed, but the data data are saved ( 3 ) immediately (no wait here).
To reach what I need, I had to change the logic.
New Logic
I had to decouple the check logic from the save:
No change to the check ( 1 )
Move the save of the data ( 3 ) in a separated function (SaveData())
Give out the swal ( 2 ) and then call SaveData() ( 3 ) from the swal functions.
My code to the swal:
if ((AvatarHochgeladen) && (!(AvatarUebernommen))) {
swal.fire({
title: '! Achtung !',
html: 'Sie haben eine eigene Grafik hochgeladen, diese aber nicht mit dem Button "Ihren upload übernehmen" als aktuellen Avatar übernommen. <br> Wenn Sie die Daten so speichern, geht Ihr hochgeladener Avatar verloren. <br> Wollen Sie Ihre hochgeladene Grafik als Avatar speichern?',
icon: 'question',
showCancelButton: true,
confirmButtonColor: 'blue',
cancelButtonText: 'Nein',
confirmButtonText: 'Ja',
}).then((result) => {
if (result.value) {
AvatarSpeichern = AvatarBase64;
SaveUserdata();
}
else { SaveUserdata(); }
})
}
So.. if the user chose "Yes", I replace a variable and call SaveData(), if the user chose "No" I directly call SaveData().
My conclusion:
As swal is "non blocking" (don't stop a running JS script), we have to split the logic and call (from the user choice depending) code from swal internal.