Returning works in mysterious ways :P

Okay, maybe this can help you get a grasp of what you are doing..

Fire up your node REPL. 


By executing node without any arguments from the command-line you will be 
> dropped into the REPL.


And try this: 

function  test (a, cb) {
console.log("inside test function.. test parameter value is: ", a);
console.log("Back to test again, the value is returned from the callback: 
", cb(a+1));
}

test(0, function (b) {
console.log("inside the anonymous function provided as callback to test");
console.log("the value of anonymous parameter is: ", b);
return b+1;
});

The anonymous function returned value only has meaning because the function 
that called it is expecting a value.
The function that accepts a callback is the one that receives the return 
value from the callback.

This example was only to clarify who gets your returned value.

Another point: observe that the return statement of the function you pass 
as a callback to connection.query
is *not* the return of your main function -code that follows the callback 
in the outermost braces also gets executed.

Oh, and it doesn't matter (at least regarding the return statement) whether 
you provide a named or an anonymous function
as a callback. Try this:

copy paste:
function  test (a, cb) {
console.log("inside test function.. test parameter value is: ", a);
console.log("Back to test again, the value is returned from the callback: 
", cb(a+1));
}

and then: 
function namedFunction (b) {
console.log("inside \'namedFunction\' provided as callback to test 
function..");
console.log("namedFunction parameter has the value: ", b);
return b+1;
}

and run: 
test(0, namedFunction);



On Wednesday, May 21, 2014 4:22:55 PM UTC+3, Janos wrote:
>
> Hi there,
>
>
> [..]
>

-- 
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/8f463b62-b06c-4618-81f6-21a8fa76c6d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to