shau
shau

Reputation: 153

Run async process in Node.js

I understand that node.js has a lot of functions that runs async, like the I/O functions. I am trying to run two modules that I created in a async way, these modules only use the CPU, not an I/O request.

For example, I have something like this: I call f2() first, but I want it so if f1() ends first it won't wait until f2() has finished; is that possible? This is only an example, but I will use this calling another js file.

I tried to write this using the process.nextTick(), but I don't know if that's the real solution.

function f1(){

    for (i=0;i< 100;i++){

    }
    console.log("f1")
}

function f2(){

    for (i=0;i< 100000000000;i++){

    }
    console.log("f2")
}


f2();
f1();

Upvotes: 1

Views: 2366

Answers (1)

KeepCalmAndCarryOn
KeepCalmAndCarryOn

Reputation: 9075

There is an async module for doing just this

look at the parallel example in the following post node.js async libs

async.parallel([
    function(){ ... },
    function(){ ... }
], callback);

the wiki for async.js https://github.com/caolan/async

Upvotes: 1

Related Questions