Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It really depends when in the process do you assign to name. <br/></p> <p>if between assigning the name to calling requestListener, there is an async method, then you we'll have "race conditions" (I.E. two threads changing the same object at the same time) even though node.js is single-threaded. <br/> this is because node.js will start processing a new request while the async method is running in the background. <br/></p> <p>for example look at the following sequence:</p> <pre><code>request1 starts processing, sets name to 1 request1 calls an async function node.js frees the process, and handles the next request in queue. request2 starts processing, sets name to 2 request2 calls an async function node.js frees the process, the async function for request 1 is done, so it calls the callback for this function. request1 calls requestListener, however at this point name is already set to 2 and not 1. </code></pre> <p>dealing with Async function in Node.js is very similar to multi-threaded programming, you must take care to encapsulate your data. in general you should try to avoid using Global object, and if you do use them, they should be either: immutable or self-contained. <br/></p> <p>Global objects shouldn't be used to pass state between functions (which is what you are doing).</p> <p>The solution to your problems should be to put the name global inside an object, the suggested places are inside the request object, which is passed to all most all functions in the request processing pipelie (this is what connect.js,express.js and all the middleware are doing), or within a session (see connect.js session middleware), which would allow you to persist data between different requests from the same user.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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