Thanks Tim. That cleared up a lot of misconceptions, especially regards callback functions.
Just love this community :) On Wed, Jun 6, 2012 at 7:49 PM, Tim Caswell <[email protected]> wrote: > Anand, it appears some fundamentals about closures, scope, and callbacks > are confusing you. Some tips that may help. > > 1. Always use `var` when creating a new local variable or it will be > declared global and clobber existing variables. > 2. Your `result` argument is a function. Every time you call it, it's > body will be executed. > 3. Node style callback functions should be of the form (err, result) and > only be called once. The err argument is usually either `null` or an > instance of `Error`, not `true` or `false` > 4. The return value of callbacks usually doesn't matter and indeed in your > case, you're not returning anything from the callback. Then what are you > trying to store in `this.result` > 5. `this` is `undefined` or the global scope (depending on if you're using > strict mode) for normal functions not called as part of an object. In this > case it's global scope. > > Good luck continuing to learn the wonderful language of JavaScript :) > > > On Wed, Jun 6, 2012 at 9:12 AM, Anand George <[email protected]>wrote: > >> Jose. That's what I started doing and it was giving expected results >> >> 3 >> 6 >> >> Then I was just trying out some testing to see the output in each case >> and was surprised to find this. The other options I tried are these: >> >> function addasync(no1, no2, result) >> { >> res = no1 + no2; >> }; >> >> returns >> >> 6. >> >> Here result is not being updated and therefore the console does not log >> the output from this function. So in this case the calling function is >> running even without getting it's callback updated. >> >> In the next case >> >> function addasync(no1, no2, result) >> { >> res = no1 + no2; >> result = result(false, res); >> process.nextTick(function() { >> }); >> }; >> >> it returns >> >> 3 >> 6 >> >> It is running like a synchronous function. The function is running with >> both parameters available and thus the console log. >> >> The third case is the one I listed initially. Here the function reruns >> after its callback parameter is updated. Is this expected behavior. >> >> >> On Wed, Jun 6, 2012 at 7:23 PM, José F. Romaniello < >> [email protected]> wrote: >> >>> this is crazy >>> >>> i think that what Anand is might want to do is this: >>> >>> >>> function addAsync(no1, no2, cb) >>> { >>> process.nextTick(function() { >>> >>> res = no1 + no2; >>> cb(null, res); >>> }); >>> }; >>> >>> addAsync(1, 2, function(err, res) { >>> console.log(res); >>> }); >>> >>> in order to queue the *intensive CPU operation of adding two numbers*for >>> the next tick in the event loop.. >>> >>> >>> >>> 2012/6/6 Oliver Leics <[email protected]> >>> >>>> On Wed, Jun 6, 2012 at 3:31 PM, Anand George <[email protected]> >>>> wrote: >>>> > When I run this code I get the following output. >>>> > >>>> > 3 >>>> > 6 >>>> > 3. >>>> > >>>> > Any ideas why this could be happening. >>>> >>>> This happens because the code does what it has to do. >>>> >>>> > It's forcing the calling function to run again. >>>> >>>> I don't understand that. What did you expect? Maybe only one line with >>>> 3? But the code calls the callback two times. >>>> >>>> ??? >>>> >>>> :-) >>>> >>>> -- >>>> 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 >>>> >>> >>> -- >>> 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 >>> >> >> -- >> 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 >> > > -- > 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 > -- 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
