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

221
Visualizações
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 à 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