One way to look at this is that in async code, indentation means
"later in time".
console.log('first');
asyncFunction(function(er, data) {
console.log('third');
anotherAsync(function(er, data2) {
console.log('fifth');
});
console.log('fourth');
});
console.log('second');
So, whatever the asyncFunction's callback returns, it's way too late.
That won't return now. It will return WAAAAYAYYYYY in the future
sometime, after EONS and CENTURIES have passed by, perhaps. Long
enough to poll sockets, read from files, spin hard drives, etc. The
tick is over. The stack is unwound.
What to do in this situation? Fetch what you need, and THEN do
something with it. Don't return; take a callback. It's callbacks all
the way down.
To make it clear what's going on, I've avoided using any "return"
statements in the example below. To "return" a result, you just pass
it to the callback.
// comments.js
exports.commentCount = function (debateId, cb) {
// this "getComments" function hits the database or whatever.
// i don't actually know what you're doing. Fill in with your own junk.
getComments(debateId, function (er, comments) {
// never ignore errors!
if (er) cb(er);
else if (!comments) cb(null, 0);
else cb(null, comments.length);
});
};
// server.js
// the route thingie. could use express or some
// other clever thing for this. reduced for illustration.
var comments = require('./comments.js');
http.createServer(function(req, res) {
var re = /^\/comment-count\/([^\/]+)$/;
if (req.method === 'GET') {
var match = req.url.match(re);
if (match) {
var debate = match[1];
comments.commentCount(debate, function(er, count) {
if (er) {
res.statusCode = 500;
res.end('a bad happend');
}
res.statusCode = 200;
res.end('Comment count = ' + count);
});
} else {
res.statusCode = 404;
res.end('not found');
}
} else {
res.statusCode = 405;
res.end('This server only supports GETs, sorry');
}
}).listen(PORT);
On Wed, Feb 20, 2013 at 11:26 AM, greelgorke <[email protected]> wrote:
> this version will just return the result of getComments, which seem to be
> an async function. so anything that might be returned from it will be not
> the comments.
>
> Am Mittwoch, 20. Februar 2013 19:00:45 UTC+1 schrieb Sergey Jamy:
>>
>> As i see you exports function "comments" which doesn't have self "return".
>> Maybe this work:
>>
>> exports.comments = function (debate) {
>> return debate.getComments(function(err,comments){
>> console.log(comments.length);
>> return comments.length;
>> });
>> };
>>
>> понедельник, 18 февраля 2013 г., 21:16:51 UTC+2 пользователь Eric Ponce
>> написал:
>>>
>>> Hi guys!
>>>
>>> I'm working in a Geddy.JS application and I have one problem with one
>>> function.
>>>
>>> In my view(in EJS) I have the next:
>>>
>>> <%= h.comments(debate); %>
>>>
>>> My problem: When we go to the "comments" function, we have:
>>>
>>> exports.comments = function (debate) {
>>> debate.getComments(function(err,comments){
>>> console.log(comments.length);
>>> return comments.length;
>>> });
>>> };
>>>
>>> My app doesn't print anything, but CONSOLE.LOG prints all perfect.
>>> How can I return the value and print it into the view?
>>>
>>> Thanks to all!!!!
>
> --
> --
> 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.