Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

360
Visualizações
How to create an asynchronous confirmation box from an ajax function in javascript?

I am trying to create a custom confirmation box that will run asynchronously from an ajax function that will call a hidden Delete button in the C# code behind, but I can't seem to get it to wait properly. I simply want it to bring up a confirmation box and then wait to process anything else until the user has chosen yes or no. I am using a Promise for waiting. However, it doesn't seem to be doing anything. The hidden Delete button click should be the last thing that happens if the user has clicked yes, but the code is not getting into that block. I am sure there is something very simple that I am missing. The following is the code that I am using:

<div id="confirm" class="confirm">
    <div class="message">The project name already exists!  Do you wish to delete the project?</div>
    <br />
    <div>
        <button id="true_btn" class="true_btn">Yes</button>
        <button id="false_btn" class="false_btn">No</button>
    </div>
</div>

function checkDirectory(projectName) {
        var onError = false;
        $.ajax({
            type: 'GET',
            url: "<%= ConfigurationManager.AppSettings["checkDirUrl"] %>" + "/" + projectName,
            //async: false,
            success: async function (result) {
                if (result === "folder exists")
                {
                    //if (confirm('The project name already exists!  Do you wish to delete the project?')) {
                    if (await deleteDemo() === 1) {
                        alert('After deleteDemo');
                        $("#<%= btnHiddenDelete.ClientID %>").click();
                    } else {
                        onError = true;
                        $("#error").text("Project name already exists!");
                    }
                } else {
                    window.location = ("/Database.aspx");
                }
            },
            error: function (result) {
                onError = true;
                $("#error").text("Error accessing project folder!");
            }
        });
        return onError;
    }

async function confirmationBox() {
        return new Promise(function(resolve, reject) {
            var confirmation_modal = $("#confirm");
            var true_el = $("#true_btn");
            var false_el = $("#false_btn");

            alert('In confirmationBox');

            confirmation_modal.show();

            true_el.click(function() {
                alert('yes');
                resolve(1);
            });

            false_el.click(function() {
                alert('no');
                resolve(0);
            });
        });
    }

    async function deleteDemo() {
        var result = await confirmationBox();
        alert(result);
        
        return result;
    }
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda