No value returned from seetalert2, undefined
See original GitHub issueLet’s say I have this code:
Confirm function
function confirmFn() {
return swal({
title: 'Confirmation',
html: ' Are you sure? ',
type: 'warning',
showCancelButton: true,
closeOnConfirm: true,
closeOnCancel: true
}, function (isConfirm) {
if (isConfirm) {
return true;
} else {
return false;
}
});
}
If you call the function, the return value should be true or false
var reternedValue = confirmFn() ;
alert(reternedValue); // undefined
Then sweetalert popups correctly but it doesn’t hold the script waiting for the user’s action, confirm or cancel.
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to return the value of SweetAlert2 input? - Stack Overflow
I've tried looking for a bunch of solutions but I always get undefined or the promise itself returned, have no idea how I'm...
Read more >SweetAlert2 - a beautiful, responsive, customizable and ...
Validator for input field, may be async (Promise-returning) or sync. Returned (or resolved) value can be: a falsy value (undefined, null, false) for...
Read more >SweetAlert2 (Advance version of SweetAlert) - GeeksforGeeks
Let's build a react project and show the working of sweetAlert2 ... An Alert will be popUp which will ask to increment the...
Read more >How to Customize Sweetalert2 on delete - Laracasts
You see "undefined" because all variables in JS that is not defined, has "undefined" value. Another way that you can do is pass...
Read more >Sweet Alert 2 adapter for PHP flasher
Validator for input field, may be async (Promise-returning) or sync. Returned (or resolved) value can be: a falsy value (undefined, null, false) ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@paschalidi This issue is old, from back before the library moved to using Promises instead. The same basic problem still applies though - you’re trying to use
swal({ ... })
as a synchronous function, while user input by definition is an asynchronous action.Here’s an example of how to work with the “new” Promise API:
A bunch more examples are in the readme.
On a closing note: @paschalidi this has little to do with SweetAlert2, and more to do with Promises. You should read up on those instead.