In angularjs ngDialog.openConfirm dialog appears after whole function execution

Problem

In angularjs I want to show confirm dialog on delete operation my code is below :

function deleteOperation(){
    var result;        
             ngDialog.openConfirm({
                    template:
                        '<p>Are you sure you want to delete selected conversation(s) ?</p>' +
                        '<div>' +
                          '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No&nbsp;' +
                          '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
                        '</button></div>',
                    plain: true,
                    className: 'ngdialog-theme-default'
                }).then(function (value) {
                    result=true;
                }, function (value) {
                    result=false;
                });

            if (result == true) {
                // perform delete operation
 }
}

But dialog is displayed after whole function execution, so in if condition result variable get undefined value

Problem courtesy of: Shb

Solution

Callback are asynchronous. So, you have to perform your operation in your success callback like this:

function deleteOperation(){     
             ngDialog.openConfirm({
                    template:
                        '<p>Are you sure you want to delete selected conversation(s) ?</p>' +
                        '<div>' +
                          '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No&nbsp;' +
                          '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
                        '</button></div>',
                    plain: true,
                    className: 'ngdialog-theme-default'
                }).then(function (value) {
                    // perform delete operation
                }, function (value) {
                    //Do something 
                });
    }
Solution courtesy of: Pierre-Alexandre Moller

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.