Re: [nodejs] process.nextTick in a callback

2012-06-06 Thread Diogo Resende
The _this_ inside process.nextTick seems quite odd to me.. -- Diogo Resende On Wednesday, June 6, 2012 at 14:31 , Anand George wrote: > Hi! > > Have been reading up on process.nextTick and found this one rather odd. It's > forcing the calling function to run again. > > function addasync(no1

Re: [nodejs] process.nextTick in a callback

2012-06-06 Thread Anand George
Even without the 'this' inside process.nextTick it returns the same result. On Wed, Jun 6, 2012 at 7:05 PM, Diogo Resende wrote: > The _this_ inside process.nextTick seems quite odd to me.. > > -- > Diogo Resende > > On Wednesday, June 6, 2012 at 14:31 , Anand George wrote: > > Hi! > > Have been

Re: [nodejs] process.nextTick in a callback

2012-06-06 Thread Oliver Leics
On Wed, Jun 6, 2012 at 3:31 PM, Anand George 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

Re: [nodejs] process.nextTick in a callback

2012-06-06 Thread José F . Romaniello
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 o

Re: [nodejs] process.nextTick in a callback

2012-06-06 Thread Angel Java Lopez
The first call to addasync is running in sync way (hmmm... I should write a better explanation, but my English is limited ;-)...) Then, it prints the first 3. The call to addsyn is runnning after that, then it prints the 6 Meanwhile, the first addasync queued a new call in process.nextTick. In the

Re: [nodejs] process.nextTick in a callback

2012-06-06 Thread Tim Caswell
Here is a slightly cleaned up version of the code with comments as to why it's doing what it does. (the `this.result` code didn't affect the output and was a distraction since it was polluting the global scope) function addasync(no1, no2, callback) { res = no1 + no2; callback(false, res); // W

Re: [nodejs] process.nextTick in a callback

2012-06-06 Thread Anand George
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

Re: [nodejs] process.nextTick in a callback

2012-06-06 Thread Tim Caswell
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 i

Re: [nodejs] process.nextTick in a callback

2012-06-06 Thread Anand George
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 wrote: > Anand, it appears some fundamentals about closures, scope, and callbacks > are confusing you. Some tips that may help. >