Trouble dealing with asynchronous non-blocking model

I'm having trouble with getting around asynchronous model of node, I have this function:

function getstream() {

    console.log('calling testAvail, Avail value is:' + available);
    testAvailability();
    console.log('Available:'+available);
    if (available || SelfSharing) {

         // Do something
         setDefaults();

         return;
    }
}

Which calls testAvailability() function defined as:

function testAvailability()
{
    console.log('entered test');
    var stat;
    var socket = io.connect('http://somedomain.com');
    socket.on('error', function() {

        console.log('There was an error at server end please try again');
        // chrome.runtime.reload();

        setTimeout(chrome.runtime.reload(),3000);
        //here i change options
        //socket = io.connect(host, options);
    });
    socket.emit('available');
    socket.on('available', function (status) {
        console.log('got status from server which is:'+ status);
        available=status;
        console.log("inside the socket.on"+available);
        console.log('leaving test, do you see any got status above?');
    });
}

I want getstream() to continue from where it left after testAvailability() is done and available variable has it's value set.

You need to pass a callback to testAvailability and then call it inside socket's available event:

function testAvailability(callback)
{
    console.log('entered test');
    var stat;
    var socket = io.connect('http://somedomain.com');
    socket.on('error', function() {

        console.log('There was an error at server end please try again');
        // chrome.runtime.reload();

        setTimeout(chrome.runtime.reload(),3000);
        //here i change options
        //socket = io.connect(host, options);
    });
    socket.emit('available');
    socket.on('available', function (status) {
        console.log('got status from server which is:'+ status);
        available=status;
        console.log("inside the socket.on"+available);
        console.log('leaving test, do you see any got status above?');

        callback(); <-- CALL YOUR CALLBACK FUNCTION HERE
    });
}

Then change your getstream function so that it passes a callback to testAvailability and move the rest of the function inside that callback:

function getstream() {

    console.log('calling testAvail, Avail value is:' + available);
    testAvailability(function() {
        console.log('Available:'+available);
        if (available || SelfSharing) {

             // Do something
             setDefaults();
        }
    });
}

A couple of things to note:

  1. getstream is now asynchronous. If something else is waiting for it to complete, you'll need to change it so that it takes a callback function as well.
  2. I see that you're doing some sort of retry in socket's error event. That might be ok in this case, it depends on your app but, typically, you'll want to pass the error back as the first parameter of the callback when an error occurs.

Here we go:

function getstream() {

    console.log('calling testAvail, Avail value is:' + available);
    testAvailability(function(available){
        console.log('Available:'+available);
        if (available || SelfSharing) {

             // Do something
             setDefaults();

             return;
        }
    });
}

function testAvailability(callback) {
    console.log('entered test');
    var stat;
    var socket = io.connect('http://somedomain.com');
    socket.on('error', function() {

        console.log('There was an error at server end please try again');
        // chrome.runtime.reload();

        setTimeout(chrome.runtime.reload(),3000);
        //here i change options
        //socket = io.connect(host, options);

        callback(false);
    });
    socket.emit('available');
    socket.on('available', function (status) {
        console.log('got status from server which is:'+ status);
        console.log('leaving test, do you see any got status above?');
        callback(true);
    });
}