[nodejs] process.nextTick in a callback
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, no2, result) { res = no1 + no2; this.result = result(false, res); process.nextTick(function() { this.result = result(true, res); }); }; addasync(1, 2, function(err, res) { console.log(res); }); function addsync(no1, no2) { return (no1 + no2); } console.log(addsync(4, 2)); When I run this code I get the following output. 3 6 3. Any ideas why this could be happening. -- 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
Re: [nodejs] process.nextTick in a callback
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, no2, result) { res = no1 + no2; this.result = result(false, res); process.nextTick(function() { this.result = result(true, res); }); }; addasync(1, 2, function(err, res) { console.log(res); }); function addsync(no1, no2) { return (no1 + no2); } console.log(addsync(4, 2)); When I run this code I get the following output. 3 6 3. Any ideas why this could be happening. -- 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 nodejs@googlegroups.com (mailto:nodejs@googlegroups.com) To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com (mailto:nodejs+unsubscr...@googlegroups.com) 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
Re: [nodejs] process.nextTick in a callback
Even without the 'this' inside process.nextTick it returns the same result. On Wed, Jun 6, 2012 at 7:05 PM, Diogo Resende drese...@thinkdigital.ptwrote: 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, no2, result) { res = no1 + no2; this.result = result(false, res); process.nextTick(function() { this.result = result(true, res); }); }; addasync(1, 2, function(err, res) { console.log(res); }); function addsync(no1, no2) { return (no1 + no2); } console.log(addsync(4, 2)); When I run this code I get the following output. 3 6 3. Any ideas why this could be happening. -- 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
Re: [nodejs] process.nextTick in a callback
On Wed, Jun 6, 2012 at 3:31 PM, Anand George mranandgeo...@gmail.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
Re: [nodejs] process.nextTick in a callback
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 oliver.le...@gmail.com On Wed, Jun 6, 2012 at 3:31 PM, Anand George mranandgeo...@gmail.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
Re: [nodejs] process.nextTick in a callback
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 next tick, that call will be run, producing the second 3 Make sense? On Wed, Jun 6, 2012 at 10:31 AM, Anand George mranandgeo...@gmail.comwrote: 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, no2, result) { res = no1 + no2; this.result = result(false, res); process.nextTick(function() { this.result = result(true, res); }); }; addasync(1, 2, function(err, res) { console.log(res); }); function addsync(no1, no2) { return (no1 + no2); } console.log(addsync(4, 2)); When I run this code I get the following output. 3 6 3. Any ideas why this could be happening. -- 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
Re: [nodejs] process.nextTick in a callback
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); // Will log the first 3 process.nextTick(function () { // nextTick returns right away, so we don't get 3 yet callback(true, res); // Will log the last 3 }); }; function addsync(no1, no2) { return (no1 + no2); } addasync(1, 2, function(err, res) { console.log(res); // Will get called twice, once before returning and once in the nextTick }); console.log(addsync(4, 2)); // Will log the 6 in the middle On Wed, Jun 6, 2012 at 8:31 AM, Anand George mranandgeo...@gmail.comwrote: 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, no2, result) { res = no1 + no2; this.result = result(false, res); process.nextTick(function() { this.result = result(true, res); }); }; addasync(1, 2, function(err, res) { console.log(res); }); function addsync(no1, no2) { return (no1 + no2); } console.log(addsync(4, 2)); When I run this code I get the following output. 3 6 3. Any ideas why this could be happening. -- 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
Re: [nodejs] process.nextTick in a callback
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 jfromanie...@gmail.comwrote: 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 oliver.le...@gmail.com On Wed, Jun 6, 2012 at 3:31 PM, Anand George mranandgeo...@gmail.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
Re: [nodejs] process.nextTick in a callback
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 mranandgeo...@gmail.comwrote: 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 jfromanie...@gmail.com 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 oliver.le...@gmail.com On Wed, Jun 6, 2012 at 3:31 PM, Anand George mranandgeo...@gmail.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
Re: [nodejs] process.nextTick in a callback
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 t...@creationix.com 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 mranandgeo...@gmail.comwrote: 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 jfromanie...@gmail.com 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 oliver.le...@gmail.com On Wed, Jun 6, 2012 at 3:31 PM, Anand George mranandgeo...@gmail.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en -- Job Board: http://jobs.nodejs.org/