async / await + jquery ajax

by jstenkvist

HTML

<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

JavaScript

function myPageUnsaved(url, height, width) {
    var defer = $.Deferred();
    $('<div></div>')
    .html('You have unsaved changes. Leave this page and lose your changes?')
    .dialog({
        autoOpen: true,
        modal: false,
        height: height,
        width: width,
        title: 'Confirmation',
        buttons: {
            "No": function () {
                defer.resolve("false");
                $(this).dialog("close");
             },
             "Yes": function () {
                defer.resolve("true");
                $(this).dialog("close");
             }
        },
        close: function () {
            $(this).dialog('destroy').remove();
            defer.resolve("false");
        }
    });
    return defer.promise();
}

myPageUnsaved("http://localhost/fng/popup.do?DisplayText=Leveransdatum har redan passerat. Vill du ändra i alla fall?", 150, 240).then(function (answer) {
  alert(answer)
});