Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think, what you want is:</p> <pre><code>function foo() { while (count &lt; 10) { process.nextTick(doSometing); count ++; } } </code></pre> <p><code>process.nextTick</code> will schedule the execution of <code>doSometing</code> on the next tick of the event loop. So, instead of switching immediately to <code>doSometing</code> this code will just schedule the execution and complete <code>foo</code> first.</p> <p>You may also try <code>setTimeout(doSometing,0)</code> and <code>setImmediate(doSometing)</code>. They'll allow <code>I/O</code> calls to occur before <code>doSometing</code> will be executed.</p> <h1>Passing arguments to <code>doSomething</code></h1> <p>If you want to pass some parameters to <code>doSomething</code>, then it's best to ensure they'll be encapsulated and won't change before <code>doSomething</code> will be executed:</p> <pre><code>setTimeout(doSometing.bind(null,foo,bar),0); </code></pre> <p>In this case <code>doSometing</code> will be called with correct arguments even if <code>foo</code> and <code>bar</code> will be changed or deleted. But this won't work in case if <code>foo</code> is an object and you changes one of its properties.</p> <h1>What the alternatives are?</h1> <p>If you want <code>doSomething</code> to be executed in parallel (not just asynchronous, but actually in parallel), then you may be interested in some job-processing solution. I recommend you to look at <a href="https://github.com/verbling/kickq" rel="nofollow">kickq</a>:</p> <pre><code>var kickq = require('kickq'); kickq.process('some_job', function (jobItem, data, cb) { doSomething(data); cb(); }); // ... function foo() { while (count &lt; 10) { kickq.create('some_job', data); count ++; } } </code></pre> <p><code>kickq.process</code> will create a separate process for processing your jobs. So, <code>kickq.create</code> will just register the job to be processed.</p> <p><code>kickq</code> uses <a href="http://redis.io/" rel="nofollow">redis</a> to queue jobs and it won't work without it.</p> <h1>Using node.js build-in modules</h1> <p>Another alternative is building your own job-processor using <a href="http://nodejs.org/api/child_process.html" rel="nofollow">Child Process</a>. The resulting code may look something like this:</p> <pre><code>var fork = require('child_process').fork, child = fork(__dirname + '/do-something.js'); // ... function foo() { while (count &lt; 10) { child.send(data); count ++; } } </code></pre> <p><code>do-something.js</code> here is a separate <code>.js</code> file with <code>doSomething</code> logic:</p> <pre><code>process.on('message', doSomething); </code></pre> <p>The actual code may be more complicated.</p> <h1>Things you should be aware of</h1> <p><code>Node.js</code> is single-threaded, so it executes only one function at a time. It also can't utilize more then one CPU.</p> <p>Node.js is asynchronous, so it's capable of processing multiple functions at once by switching between them. It's really efficient when dealing with functions with lots of <code>I/O</code> calls, because it's newer blocks. So, when one function waits for the response from DB, another function is executed. But <code>node.js</code> is not a good choice for blocking tasks with heavy CPU utilization.</p> <p>It's possible to do real parallel calculations in <code>node.js</code> using modules like <a href="http://nodejs.org/api/child_process.html" rel="nofollow">child_process</a> and <a href="http://nodejs.org/api/cluster.html" rel="nofollow">cluster</a>. <code>child_process</code> allows you to start a new <code>node.js</code> process. It also creates a communication channel between parent and child processes. Cluster allows you to run a cluster of identical processes. It's really handy when you're dealing with <code>http</code> requests, because <code>cluster</code> can distribute them randomly between workers. So, it's possible to create a cluster of workers processing your data in parallel, though generally <code>node.js</code> is single-threaded.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload