1

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?

3
  • 1
    Do you want to be able to programatically detect a blocking function (impossible inadvance) or want to know how to visually identify a blocking function (possible)? Commented May 23, 2014 at 15:22
  • Or do you want to be able to know if a callback was executed asynchronously or synchronously with the surrounding code? Commented May 23, 2014 at 17:13
  • I want to know which functions are blocking timers, that have to be executed. It's online game, and timers are respawning some objects. I'v noticed that loops, even foreach, blocking timers. Also some functions. So I need to have all functions noblocking. I actualy thought that node.js is abit better in such things, but with mongodb it seems to be not that good for online games :P Commented May 24, 2014 at 10:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Also, you might want to look into node-async
1

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); }; 

2 Comments

I thougth that nonblocking function in JS, has to be made on timers, set to 0. Because if it will just have callback, it still will be blocking. Also if you put blocking things in nonblocking function it will start to block right? For example put infinity while loop, it will block timers and other nonblocking functions, even if it will have callback.
Also I wrote three functions. First, second and someCallback. I put 1000 callbacks into first function. And it blocked second functions, even if it were on callbacks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.