On Wednesday, July 11, 2012 10:23:55 AM UTC-7, Alex Tian wrote:
>
> Hello everyone, 
>
> I am trying to test nodejs with mysql, I think due to its non-blocking 
> nature. I have a difficult time to add the query result into a output 
> buffer (var output in following code). Can anybody point me some way? 
>

You seem to be missing one important concept.  Callbacks don't interrupt 
the currently executing function.  In fact, they don't interrupt anything, 
they'll wait until the current thread of execution (tick) is complete and 
then run.  Because of this, you can't reference the output of a callback 
within the function (or even the tick) that sets up the callback.

Think of it this way.  The V8 engine is going to execute your function in a 
single tick.  It's going to start at the top and work its way down, 
following normal control flow.  When you call a function that invokes a 
callback (almost any function that *MAY* require I/O), that callback 
doesn't happen until the I/O happens, so the engine continues processing 
your function.  So 

    output += '</body>\n</html>'; 
>     res.end(output); 


runs before any of the code in the callback, so that data hasn't been 
appended to output at the time you send it with res.end();

The way to do this is to gather the data with the callbacks, and then 
either output the data within the callbacks, or pass the data to a function 
that will output the data.

-- 
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to