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

124
Visualizações
Waiting for Async Request to finalize before doing something in javascript

I am working on a web app that uses Indexed DB to work. I want to check if an object with that ID exists in my Database. This is my actual code:

function checkKeyExists(id){
    request = window.indexedDB.open("ManagementDB", 1);
    let exists = false;
    request.onerror = function (event) {
        window.alert("Error using indexedDB");
        return false
    }
    request.onsuccess = function (event) {
        let getRequest = request.result.transaction("KeyStorage", "readonly").objectStore("KeyStorage").get(id)
        getRequest.onerror = function(event){
            console.log("Info: checkKey: error reading key "+ id+ " : " + event);
        }
        getRequest.onsuccess = function(event){
            if (typeof(getRequest.result) === 'object'){
                keyStatus = true;
            }
        }
    }
    
    // Need to wait for everything to finish before doing the return 
    return keyStatus
}

I do not know how to wait for all of the code to finish before executing the return. I am still a starter in javascript, it might be an easy question, but I did not get any response to this looking at other questions. Other questions uses await, but that would not resolve anything for me.

about 4 years ago · Juan Pablo Isaza
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