Q:

Creating a waiting function using process.nextTick or setImmediate - node.js

I was coding some Node.js and I thought, coming from a Java background, that I should figure out how to create my own "wait" functionality. Obviously, it will be a little different since Java is designed to be multi-threaded and Node is not. Anyway, the desired functionality I want in Node is that I want to create a blocking function myself - such that if some boolean in the program is not set, I might want to call process.nextTick() until the boolean gets set.
So it would be:
    var bool = false;

    function foo(){
    var b = baz();
    }

    function baz(){

        while(!bool){
           process.nextTick(function() {
           return baz(arguments);
        });
      }

    return {};
    }

   function setBool(number) {

       setTimeout(function () {
            bool = false;
       }, number);
   }

   process.nextTick(wait);
   setBool(3000);

so this question is twofold:
(1) is this the best way to implement a waiting type functionality in Node.js?
(2) If I called baz(arguments) instead the baz function (recursively) what happens in terms of the execution? Generally, when you want the return value from function X, but function X calls itself a few times before returning, does this pose any problems? Is it no big deal and it turns out to be the same return value? I have thought about this before but never really worried about it or investigated it.

A:

Do not 'wait' in Node.js. "Waiting" blocks ALL other execution. That's an important note because if you block, then the boolean can NEVER set changed. JavaScript, and especially in Node, uses an event driven model. Do this, when that.
So the best way? It's opinion based but this is one way:
var bool = false;

function foo() {
    console.log('Listening for true');
    baz(function() {
        //Do something when called
        console.log('It\'s finally true!');
    });
}

function baz(callback) {
    setImmediate(function() {
        if (bool)
            callback();
        else
            baz(callback);
    });
}

foo();
setTimeout(function() {
    console.log('Setting to true');
    bool = true;
}, 4000);

A:

The other answers have already stated the important part: No, this will not work. I wanted to post this to provide a little more background on why this will not work.
Under the hood, node.js is running an event dispatching loop. There is a queue of events (including, at least conceptually, "app start", "incoming message", "socket connection", "file io complete", etc.). As events happen, the event dispatcher pulls the event off the queue and calls any Javascript functions associated with that event. Once the callback functions are completed, the dispatch then pulls the next event off the queue, processes it, etc. This is completely synchronous - the next event cannot be processed until the current one has completed.
So when you have code like this:
var done = false;
while (!done) {
    process.nextTick(function () {
       done = true;
    });
}

what happens? You enter the while loop, the process.nextTick puts an event on the event queue to be processed the next time the event loop runs. Then your code loops back through the while, checks done (still false), queues another event, etc.
Since the event loop can't process the queued event until your function returns, the event loop gets stuck and the callback for process.nextTick never gets called. You've ensured there never is a "nextTick".
You cannot do a synchronous wait in pure Javascript code. In Node.js you can do it in C extensions (that's how stuff like fs.readFileSync are implemented). There are some Javascript compiler tricks you can use (like streamline or co) to write code that looks synchronous. But under the hood they're all using the async callback pattern.
My recommendation is to learn the core callback pattern first so you understand it. It is, granted, a giant pain. From there investigate promises (I personally like Bluebird as a promise implementation, or if you're using ES6 you can use the built-in promises) or one of the many async helper libraries.

