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.com>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

Reply via email to