On Aug 20, 2014, at 9:26 AM, Дмитрий Папка <[email protected]> wrote:
> Hello, everyone!
> I am new to Node.Js and not very well understanding the mecanics of callbacks.
>
[snip]
> if (long) {
> while (Date.now() < then) {}
> }
> db.users.find({}, function(err, users) {
> callback(users);
> });
[snip]
> If I am openning: http://website.com/, it loads immediately.
> If I am oppening: http://website.com/?long=something, it loads after ~ 5
> seconds as expected.
> Now. If I am openning http://website.com/?long=something and right after that
> http://website.com/ - they both are loading 5 seconds (second request is
> waiting until the first one will end).
> That means I am doing something wrong, because requests are handled not
> asyncroniously.
> What did I do wrong?
The while loop is blocking -- it spins, doing useless work and NOTHING else
until it expires. Your entire node process is hung during this time, so both
requests "wait".
Delaying with:
setTimeout(function() {
db.users.find({}, function(err, users) {
callback(users);
});
}, long ? 5000 : 0);
would do what you want, and yield the event loop in the mean time.
Node only does one thing at a time -- setTimeout is "call me when this time has
passed; do what you need to until then"; while (stuff) is "do nothing but this
until...."
Aria
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/2697E004-94C3-4B27-B3A4-E6ABB3015D2F%40nbtsc.org.
For more options, visit https://groups.google.com/d/optout.