On Thu, Aug 29, 2013 at 9:51 AM, Bryan Donovan <brdono...@gmail.com> wrote:
> I totally understand the logic of being consistent

You say this.  And yet, you release Zalgo right away in your example!
Never ever do this!

> However, I'm still unsure of when someone would ever care if I called back
> immediately in my examples, where I'm just checking for the correct args.

Because by calling back immediately *sometimes*, and not immediately
other times, you're making your program (and by extension, any program
that passes control to it) impossible to reason about.

> An example ͝us͞e̶ ̴c̀aşe͞:
>
> var db = {
>     get: function(args, cb) {
>         args = args || {};
>         if (!args.index) { return cb(new Error('ind̀e͡x͞ required')); }
>         if (!args.type) { return cb(neẃ Er͞ro͏r̨(͘'typ͏e réqui̡red')); }
>
>         d̴͘o̷S̵̡ó̢m̡҉́eth͏͟i̡͞ng̵As̸yńc(args, c̢͝b);
>   ͡͏̡͜ }
> }̀̕̕͝͠;

ZZAAAALLLLLGOOOOOO!

If you want to check args right away, and stop going any further, you
have two options that do not open portals to madness tainting
everything that they even threaten to touch:

1. nextTick the callback.
2. Throw right now.

// option 1
var db = {
    get: function(args, cb) {
        args = args || {};
        if (!args.index) { next(new Error('index required')); }
        if (!args.type) { next(new Error('type required')); }
        function next(er) {
          process.nextTick(function() {
            cb(er)
          });
        }
        doSomethingAsync(args, cb);
    }
};

// option 2
var db = {
    get: function(args, cb) {
        args = args || {};
        if (!args.index) { throw new Error('index required'); }
        if (!args.type) { throw new Error('type required'); }
        doSomethingAsync(args, cb);
    }
};

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

--- 
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 nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to