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

220
Views
Synchronizing in Node JS

I apologize if this is a bad question, I am new to node and JS in general, and I am having trouble getting the synchronization the way that I want it.

I come from languages where threading is completely controlled by the user, like C. What I am kind of looking for is something akin to a thread join to make sure we wait for our non-blocking calls to finish before continuing.

This the pseudocode for what I am trying to do:

function make_connection(){
    obj = some_kind_of_obj_used_for_connection;

    //do things to prepare obj for connection
    
    // call library function to make connection
    obj.connect(err => {
        if(err){
            //handle error
            return null;
        }else{
            return obj;
        }
    }
    console.log("I don't want to get here");
}

And I will be calling this from some other function. Lets just say some kind of test_function.

function test(){
    const returned_connection = make_connection();

    // check if null. If not, use connection

    return 0;
}

As I have learned, this will not work the way I want it to. It will call the connect function, but it will continue execution before finishing up the connection (executing my test console log) and return back to test(). Then it will use the connection before it has actually been made.

I want to know if there is a way to force synchronization on a function like "connect" in this case.

I have tried adding the async/await keywords but it is not working. What I have found on them confuse me. I assume I am using them incorrectly, if I use them like this:

async function make_connection(){
    obj = some_kind_of_obj_used_for_connection;

    //do things to prepare obj for connection
    
    // call library function to make connection
    await obj.connect(err => {
        if(err){
            //handle error
            return null;
        }else{
            return obj;
        }
    }
    console.log("I don't want to get here");
}

And I will be calling this from some other function. Lets just say some kind of test_function.

async function test(){
    await const returned_connection = make_connection();

    // check if null. If not, use connection

    return 0;
}

What is the proper way to force synchronization in a situation like this. Preferably, something that is not specific to this example of connecting, but something I can use whenever I call a library function that is non-blocking, and I want to wait on it.

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!