Here is another real world example:

var todo = 0;

function done() {
    todo--;

    if (!todo) callback();
}

todo++;

// If "a" calls back synchronously, todo amount will never be "2",  so "done" 
will call back directly after a calls back.
a(done);

if (soemthing) {
    todo++;
    b(done);
}



Best,
Oleg Slobodskoi

https://twitter.com/oleg008
https://github.com/kof

Am 21.08.2013 um 10:19 schrieb Bryan Donovan <[email protected]>:

> Thanks for the reply.
> 
> However, this is the same answer as what I keep coming across that doesn't 
> really answer my question.  What is the downside?  I want to see a real-world 
> example of what happens when I don't follow this contract. I've yet to see an 
> example of something bad happening in this situation.  I'm sure I'm wrong, 
> but I need to know *why*.
> 
> When would someone ever call:
> 
>    getSomething(args, cb);
> 
> ... and then assume they can call something else in the meantime?
> 
> 
> Thanks,
> 
> Bryan
> 
> 
> On Aug 21, 2013, at 12:32 AM, Floby <[email protected]> wrote:
> 
>> Same as above.
>> A "callback" is meant to be called back after some operations are made. In 
>> node, that means they're probably gonna get called when the current stack 
>> has unwound. If you called the callback synchronously, that means it behaves 
>> differently.
>> process.nextTick un most cases execute the given function immediately after 
>> the current stack ends (with some limitations for recursivity) so it's not 
>> really a performance killer.
>> 
>> Doing so helps you respect your function/method contract :)
>> 
>> Saying that "in most case, it's not needed" is too big of an assumption of 
>> what your user is doing with your code. IMO.
>> 
>> 
>> On Tuesday, 20 August 2013 19:47:22 UTC+2, Bryan Donovan wrote:
>> I have been writing node.js client code for a couple of years now, and have 
>> authored a couple open source libraries, but somehow I missed the memo 
>> telling me that I'm supposed to wrap 'synchrounous' callbacks in 
>> process.nextTick().  I kind-of understand why that is a best-practice, but 
>> what I don't understand is what the drawback is if you don't do it.
>> 
>> For example, I write code like this all the time, and have never had a 
>> single problem with it:
>> 
>> function getSomething(args, cb) {
>>     if (!args) { return cb(new Error('args required')); }
>>     if (!args.id) { return cb(new Error('args.id required')); }
>> 
>>     SomeDatabase.get({id: args.id}, cb);
>> }
>> 
>> What are the potential issues with not wrapping those arg checks in 
>> process.nextTick()?
>> 
>> 
>> Thanks, 
>> 
>> Bryan
>> 
>> -- 
>> -- 
>> 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
>>  
>> --- 
>> You received this message because you are subscribed to a topic in the 
>> Google Groups "nodejs" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/nodejs/0TmVfX9z1R0/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to 
>> [email protected].
>> For more options, visit https://groups.google.com/groups/opt_out.
> 
> 
> -- 
> -- 
> 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
>  
> --- 
> You received this message because you are subscribed to the Google Groups 
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.

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

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to