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

Reply via email to