5

I'm new to node.js, I tried to use setTimeout to simulate long connections and hope it act asynchronously.

var http = require('http');

http.createServer(function (request, response) {
    console.log('New request @ ' + request.url);
    (function (response) {
         setTimeout(function () {
             console.log('Time is up');
             response.writeHead(200, {"Content-Type": "text/plain"});
             response.end('Hello World\n');
         }, 3000);
     })(response);
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

But, the code above perform like a synchronous single thread app, which can only handle one request per 3 seconds.

I thought everything in node.js should act asynchronously. So, what's the problem here?

3 Answers 3

8

The SetTimeout is async, you don't need that anonym function in the middle, just write this.

var http = require('http');

http.createServer(function (request, response) {
  console.log('New request @ ' + request.url);
  setTimeout(function () {
    console.log('Time is up');
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end('Hello World\n');
  }, 3000);
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

If you produce 10 concurent request the total comp time will be around 3sec, which means it is async. You can use the ab tool to check, or if you program node, maybe easier to install http-perf. and run nperf -c 10 -n 10 http://127.0.0.1:8124

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

3 Comments

I've tested with ab, and you're right, setTimeout is an async function. But, it's a bit strange if I open several tabs in Chrome at the same time, it ends one by one between a 5 seconds interval.
I think you just not enough fast clicking between the tabs in chrome.. :)
Chrome will not request the same URL concurrently. If you add a variation to the url, e.g. the first tab requests 127.0.0.1:8124/0, the second requests 127.0.0.1:8124/1, etc, then they should behave like you expect. See stackoverflow.com/questions/15852011/…
0

You need to run your sleep in a new process. There is a module that can help you (https://github.com/cramforce/node-worker) or you can look at the normal api documentaion about spawn.

Comments

-3

var async,
__slice = [].slice;

async = require("async");

async.setTimeOut = function() {
    var arg, args, callback, delay, runWithTimeOut;
    callback = arguments[0], delay = arguments[1], arg = arguments[2], args = 4 <= arguments.length ? __slice.call(arguments, 3) : [];
    runWithTimeOut = function() {
        return setTimeout(callback, delay, arg, args);
    };

    return async.parallel([runWithTimeOut]);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.