FAQ
I understand that Node.js has the concept of event-driven, asynchronous
callbacks, by utilizing an event loop.

database.query("SELECT * FROM hugetable", function(rows) { var result =
rows; });
console.log("Hello World");

Here, instead of expecting *database.query()* to directly return a result
to us, we pass it a second parameter, an anonymous function.
Now, Node.js can handle the database request asynchronously. Provided that *
database.query()* is part of an asynchronous library, this is what Node.js
does: just as before, it takes the query and sends it to the database. But
instead of waiting for it to be finished, it makes a mental note that says
"When at some point in the future the database server is done and sends the
result of the query, then I have to execute the anonymous function that was
passed to *database.query()*."

I am trying the same with a sample code (as I am a newbie and not reached
till Node.js DB interactions):

[root@example]# cat server8.js
function myfun(noparm , afterend)
{
for ( var i =0; i < 10; i ++)
console.log("The valus is " + i);
}

function mynextfn()
{
console.log("Hello World");
}
function afterend()
{
console.log("Hello afterend");
}

myfun(0, afterend);
mynextfn();
[root@idc-bldtool01 example]# node server8.js
The valus is 0
The valus is 1
The valus is 2
The valus is 3
The valus is 4
The valus is 5
The valus is 6
The valus is 7
The valus is 8
The valus is 9
Hello World
[root@iexample]#

As such I do not see the " concept of event-driven, asynchronous callbacks,
by utilizing an event loop" ?

Can anyone please help me in implementing some basic examples?

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

Search Discussions

  • Michael Schoonmaker at Sep 29, 2012 at 6:25 pm
    Satya,
    The "event loop" doesn't matter until two things happen:

    1. You actually perform asynchronous work. (A for loop doesn't count.
    Please read
    http://nodemanual.org/latest/nodejs_dev_guide/writing_asynchronous_code.html
    )
    2. You understand *and deeply care about* the boundaries between the
    code that will be run on each "tick" of said event loop. (For now, assume
    you neither care nor understand. Then read
    http://nodemanual.org/latest/nodejs_dev_guide/writing_asynchronous_code.htmlagain,
    and identify those boundaries. [SPOILER: The only non-first-tick
    code is on lines 5-8.])

    Until then, Keep Calm and Carry On.

    -Schoon
    On Sat, Sep 29, 2012 at 2:01 AM, Satya Prakash Prasad wrote:

    I understand that Node.js has the concept of event-driven, asynchronous
    callbacks, by utilizing an event loop.

    database.query("SELECT * FROM hugetable", function(rows) { var result =
    rows; });
    console.log("Hello World");

    Here, instead of expecting *database.query()* to directly return a result
    to us, we pass it a second parameter, an anonymous function.
    Now, Node.js can handle the database request asynchronously. Provided that
    *database.query()* is part of an asynchronous library, this is what
    Node.js does: just as before, it takes the query and sends it to the
    database. But instead of waiting for it to be finished, it makes a mental
    note that says "When at some point in the future the database server is
    done and sends the result of the query, then I have to execute the
    anonymous function that was passed to *database.query()*."

    I am trying the same with a sample code (as I am a newbie and not reached
    till Node.js DB interactions):

    [root@example]# cat server8.js
    function myfun(noparm , afterend)
    {
    for ( var i =0; i < 10; i ++)
    console.log("The valus is " + i);
    }

    function mynextfn()
    {
    console.log("Hello World");
    }
    function afterend()
    {
    console.log("Hello afterend");
    }

    myfun(0, afterend);
    mynextfn();
    [root@idc-bldtool01 example]# node server8.js
    The valus is 0
    The valus is 1
    The valus is 2
    The valus is 3
    The valus is 4
    The valus is 5
    The valus is 6
    The valus is 7
    The valus is 8
    The valus is 9
    Hello World
    [root@iexample]#

    As such I do not see the " concept of event-driven, asynchronous
    callbacks, by utilizing an event loop" ?

    Can anyone please help me in implementing some basic examples?

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedSep 29, '12 at 11:26a
activeSep 29, '12 at 6:25p
posts2
users2
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2022 Grokbase