Reentrancy in JavaScript

I would like to improve my understanding of the word reenter.

Is this feature reentrant?

function* foo() { yield 1; yield 2; } 

And this one?

 function foo() { return 1; } 

And this one?

 var x = 0; function foo() { return x++; } 

And this one?

 function foo() { setTimeout(foo, 1000); } 
+6
source share
1 answer

The reentrant function is a function that can be resumed:

In computing, a computer program or subroutine is called reentrant if it can be interrupted in the middle of its execution and then safely called again ("re-entered") until its previous calls are completed.

In the / node JavaScript browser, all multiprocessor operations are cooperative (without interrupts or context switches). A regular function is always executed until completion in JavaScript. (1)

So, in your case, the only re-entry function is the first one, since it does not run its code until completion and can be resumed at a later point.

  • The second function is just a regular function.
  • The third uses an external scope, which is similar, because it allows the function to hold a certain state. This is not the same, although the function cannot be resumed.
  • The fourth one starts immediately before completion (he plans another appeal for it - but it depends on the platform, not on JavaScript).

In fact - we can say that generators allow multi-tasking in JavaScript with the syntax of the renderer. Before the generators, all the code ended.

(1) Or he never stops, but he never interrupts. Also - on common platforms. There are platforms (e.g. Rhino) that break the rule. They are very rare and do not use the same concurrency execution model as the browser / node JS. Sub>

+6
source

All Articles