Pikachu
Pikachu

Reputation: 1993

How to check if function is nonblocking?

I have to write game server in node.js. It will have a lot of loops, functions etc. I will be writing my won functions abit, but mostly I will be using others functions.

So, game server can't have any blocking functions. It can't delay timers, etc. My question is, how to check if function is nonblocking?

Upvotes: 1

Views: 599

Answers (2)

SomeKittens
SomeKittens

Reputation: 39532

How does the function 'return' it's computed value? If it uses the return statement, it's blocking:

var doBlockingStuff = function(a, c) {
  b = a * c;
  return b;
};

If it uses a callback to move forward with the computed value, it's non-blocking:

var doNonBlockingStuff = function(a, c, callback) {
  b = a * c;
  callback(null, b);
};

You may see return statements in non-blocking code, simply to halt execution. They're still non-blocking if the value computed by the function is passed to a callback:

var doNonBlockingStuff = function(a, c, callback) {
  b = a * c;
  if (b < 0) {
    var err = 'bad things happened';
    return callback(err);
  }
  return callback(err, b);
};

Upvotes: 1

Ken Wheeler
Ken Wheeler

Reputation: 1958

Log before you call the method, log in the methods callback, and log after the method. Examine the order in which your logs appear.

Upvotes: 1

Related Questions