-1
function remove(){
    console.log(xxx());
    if(xxx() != true){
        console.log(xxx());
        return;
    }

    console.log('removed');
}

function xxx(){
    SweetAlert.swal({
            title: "Are you sure?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55"},
        function(isConfirm){
            if (isConfirm) {
                return true;
            } else {
                return false;
            }
        }
    );
}

How to get return from function xxx().

That's always return undefined when i fire remove().

If return is true i want to do console.log('removed').

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Donny Gunawan
  • 363
  • 1
  • 6
  • 21
  • please check this http://oitozero.github.io/ngSweetAlert/ 5'th example – Donny Gunawan Sep 15 '15 at 08:00
  • 2
    Related: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – CodingIntrigue Sep 15 '15 at 08:02
  • thats example is using with ajax, im still not understood, can u please explain with case above :) – Donny Gunawan Sep 15 '15 at 08:45
  • `SweetAlert.swal` accepts a *callback* function. You can't return a value from a callback function to a parent function like that - [this question](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) maybe explains it better, but you need to approach by adding your own callback – CodingIntrigue Sep 15 '15 at 08:48

1 Answers1

0

As mentioned in the comments, your calling function has no access to the result of the callback function, however you can supply your wrapper xxx function with your own callback to be passed. Therein you have the choice of supplanting the callback entirely or for example pass only the action to be undertaken on success:

function remove(){
    xxx(function(){console.log('removed');});
}

function xxx(OnSuccess){
    SweetAlert.swal({
            title: "Are you sure?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55"},
        function(isConfirm){
            if (isConfirm && OnSuccess) 
                OnSuccess();
        }
    );
}
Me.Name
  • 12,259
  • 3
  • 31
  • 48