Hi people!

Aman, bad english here ;-)

But part of the answer: yes, for JavaScript (not for all the internal guts
of Nodejs) there is only one thread. One way the state is preserved is
using local variables in functions/callback and using the previous
arguments. See also JavaScript closures:

http://jibbering.com/faq/notes/closures/

for outer variables (in outer callbacks?) that are preserved too.

If you have a module variable, declared at top level:

var count;

then that variable is shared by all the callbacks

Angel "Java" Lopez
@ajlopez




On Thu, Aug 8, 2013 at 2:03 AM, aman saggar <[email protected]> wrote:

> Apologies for being ignorant here.****
>
> ** **
>
> A simple Node.js non-blocking implementation (stupid but just to show the
> point):****
>
> var http = require("http");****
>
> http.createServer(function (request, response) {****
>
>     //Query 1st Database based on user Id****
>
>     db1.query("select x,y from DB1 where userId=" + request.currentUser.id,
> function (result1) {****
>
>         //Query 2nd Database on response from 1st Database****
>
>         db2.query("select a,b from DB2 where id =" + result1.taskid + "
> and location=" + request.currentUser.locId, function (result2) {****
>
>             response.writeHead(200, { "Content-Type": "text/html" });****
>
>             //Write Reponse****
>
>             response.write("<p>Got result for </p>" +
> request.currentUser.Name + "Data: " + result2.amount);****
>
>             response.end();****
>
> ** **
>
>         });****
>
>     });****
>
> }).listen(9090);****
>
> ** **
>
> What above program does is:****
>
>
>    - 1.  Query 1st Database for requested User
>    - 2.  Query 2nd Database based on result from previous query
>    - 3.  Stiches together a response
>
> ** **
>
> ** **
>
> If the Node.js is Single Thread based, then I hope this is something
> happens:****
>
> 1.       >Gets the request from 1st user****
>
> a.       Fires non-blocking Database Access for that userId****
>
> 2.       >Serves next request from 2nd user****
>
> a.       Fires non- blocking Database Access for that userId****
>
> …………..****
>
> …………..****
>
> 3.       >Gets Event from the non-blocking Database Access for 1st user***
> *
>
> a.       Fires the second non-blocking Database Access for 1st user.****
>
> ** **
>
> ** **
>
> I have two questions here:****
>
>
>    1. 1)*How is the state is being maintained here?*
>
> ****
>
> a.       If this is single threaded model, then the with the second
> request coming, it wouldn’t have idea of the initial request or state****
>
>                                                                i.      If
> it actually dumps the information to memory and then works on, and once it
> receives event , loads state from memory ****
>
>
> I.            Then how it is different from multi-threading, which
> forking and joining.****
>
>
>    1. 2)*Who is running the non –blocking items?*
>
> ****
>
>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to