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

80
Views
JS await function with empty Promise

I have a question, I somewhat understand how Promises work but I would like to wait for a function to finish even if it doesn't return anything. I have a similar code but it doesn't wait for b to finish before starting the extra steps on a.

For example in this case I want to create tables in a sqlite db, then load data and run some tests. The thing here is that it's starting to run the tests before finishing to load the data and the arrays created in the loadData method are coming empty. I would even prefer not to return those arrays but to always wait for this process to be completed before running tests.

async function initialize(){
    try{
        console.log("Beginning connection");
        db = await connectToDB();
        console.log(`Database: ${db}`)
        db.serialize(function() {
            console.log("Creating tables")
            db.run(queries.__parent_company);
            db.run(queries.parent_company);
            db.run(queries.__sales_rep);
            db.run(queries.sales_rep);
            db.run(queries.advertiser);
            console.log("Tables created");
        });
        let response = await loadData();
        console.log("Response", response)
        testData();
    }
    catch(e){
        throw e;
    }
}

function loadData() {
    return new Promise(async (resolve, reject) => {
        console.log("Beggning data insertion");
        try{
            let insertedAdvertisers = [];
            let insertedSales = [];
            let insertedCOmpanies = [];
            data.rows.forEach(async row => {
                let advertiser = await loadAdvertisers(row);
                let sale = await loadSalesRep(row);
                let company = await loadParentCompany(row);
                insertedAdvertisers.push(advertiser);
                insertedSales.push(sale);
                insertedCOmpanies.push(company);
                console.log("HERE", insertedAdvertisers, insertedSales, insertedCOmpanies)
            })
            resolve({insertedAdvertisers, insertedSales, insertedCOmpanies});
        }
        catch(e){
            console.log(e);
            reject(e);
        }
    });
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Taking about Promises, I advice you to read about the event loop and how it works.

What happens in your code that JS knows that this is a promise so it will move it to callback queue until it finishes and continues executing your code and then push it to the stack to be executed. so if you want to execute a certain logic after an async function finishes you need to provide it as a callback or use .then()

async function a() {
  b().then(()=>{
      // your code that you want to be executed after a finishes 
})
}

This image might illustrate what I'm trying to explain

enter image description here

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