Don't wait for function to complete

Possible Duplicate:
“Spawn a thread”-like behaviour in node.js

How can I make my code continue without waiting for extensive operations to complete?

var foo = function()
{
    var i = 1000000000;
    while (i > 0)
    {
        i--;
    }
};
console.log("start");
foo();
console.log("end");

EDIT This is not for web development. I'm using node.js

In node.js, you can use process.nextTick() for this. It will defer executing the long running task until the next pass around the event loop.

var foo = function()
{
    var i = 1000000000;
    while (i > 0)
    {
        i--;
    }
};
console.log("start");
process.nextTick(foo());
console.log("end");

Keep in mind while this will allow this request to return immediately, it will still block the thread when it eventually gets executed. Node.js isn't meant for long running CPU intensive tasks, it's optimized for handling lots and lots of lightweight connections.

If you need to do CPU intensive stuff with node, you should look at a job queue (like kue or any other pub/sub implementation) where you place jobs on a queue where a second node.js process can listen for new jobs and execute them as they come in.

This will allow your main node processes to handle new requests quickly and allow a couple of background node processes to handle all of these long running jobs.