question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No value returned from seetalert2, undefined

See original GitHub issue

Let’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:closed
  • Created 8 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
birjjcommented, Aug 18, 2016

@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:

swal({ ... })
    .then(function(){
        alert("The confirm button was clicked");
    }).catch(function(reason){
        alert("The alert was dismissed by the user: "+reason);
    });

A bunch more examples are in the readme.

0reactions
birjjcommented, Aug 18, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found