0

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.

  • `await new Promise(resolve => obj.connect(() => resolve(obj))` – Jonas Wilms Oct 05 '21 at 21:09
  • I'm very confused what this line does exactly, do you think you could explain it a bit. It would appear you are making a promise for obj connect, but isn't obj.connect already making a promise on call? – SeismicSandwhich Oct 05 '21 at 21:18
  • Is "resolved" mean we are using a callback function rather than simply returning? And if so, does "resolved" need to be listed in the parameter list for the function (in this case for make_connection)? – SeismicSandwhich Oct 05 '21 at 22:26

0 Answers0