Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!