Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda