C
C
cat_crash2012-11-30 21:26:52
Computer networks
cat_crash, 2012-11-30 21:26:52

Why is the code not being executed

Good day.
Lately, I've been breaking records for writing in the NodeJS hub. So to speak, I am saturated with this engine more and more.

Having achieved some success, I again ran into the problem of the cause of which I don’t fully understand and, accordingly, I can’t solve:
Code

...
for(var b=0, len = fields.length;b<len;b++){

  jsdom.env({
    html: './dummy.html',
    src: [jquery],
    done: function (errors, window) {
      console.log('aa');
    }
  })
  
len++;
  
}

simplified for clarity.
Actually, this is a kind of prototype of an “endless cycle”. But I don’t understand why the jsdom code is not executed inside the loop, or rather, nothing is output to the console.

If anyone has any ideas why, it would be grateful if they prompted an idea - how to properly implement such an algorithm

Answer the question

In order to leave comments, you need to log in

3 answer(s)
[
[email protected]><e, 2012-11-30
@barmaley_exe

Surely this function, like (almost) everything in the world of Node.JS, works asynchronously. And since your cycle is infinite, it simply does not have processor time left to do its job.
But this is just a guess.

Q
qxfusion, 2012-12-01
@qxfusion

The answer is quite simple - the fact is that your code is not called in principle.
When you execute a function in the CURRENT context, then the node pipeline (V8) is busy executing your function - say for example - function foo()
You are trying to create a jsdom object BUT the result of the env method is asynchronous, so it will be added to the stack of future calls, BUT i.e. to. in the current context, function foo has not yet completed, the scheduler cannot move on to another job.
For example, Mozilla Rhino doesn't have this problem thanks to Java, but in V8 the threading model is exclusively single-threaded within a context.
Solution: try calling each iteration via setTimeout or process.nextTick (although of course this method will be deprecated soon) then you will get a heavily loaded pipeline, but you will still have asynchronous events.
PS If the call stack fills up infinitely with more than 50000 returns, the node will most likely crash, so don't forget to control the growth of the queue manually.

T
Tenkoff, 2013-04-27
@Tenkoff

Use nodejs.org/api/process.html#process_process_nexttick_callback

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question