Ha-ha, very good example, compare this async version it with the sync 
version below, and tell me that there's no problem with 
callbacks """if you know how to deal with it""" :D

Not saying that in even such a simple code it's extremely
easy to make small errors like missing `else` statements etc.

Also it doesn't preserve the call stack - so when you see error in console
you would have no context and no idea what happened...
And it still doesn't catch the sync errors...
And some of callbacks can be fired twice...
And with streams there are lots of events fired and should be properly 
handled...

And still many insist that it is "not a problem".

    CDN.use(config.CDN.baseUrl, function(req, res, next) {
        if(err) return next(err);
        async.waterfall([
            function (cb) {
                fs.lstat(config.base+"/cdn"+req.url, cb);
            },
            function (stats, cb) {
                if(stats.isSymbolicLink()) {
                    async.waterfall([
                        function (cb) {
                            fs.readlink(config.base+"/cdn"+req.url, cb);
                        },
                        function (linkstr, cb) {
                            log.info("Redirecting request \""+req.url+"\" 
to \""+linkstr);
                            fs.readFile(linkstr, cb);
                        },
                    ], cb);
                }
            }
        ], function (err, data) {
            if (err) return next(err);
            res.writeHead(200,{"Content-type":"text/html"});
            res.end(data);
        });
    });
    
Sync version.

    CDN.use(config.CDN.baseUrl, function(req, res, next) {
      var stats = fs.lstat(config.base+"/cdn"+req.url)
      if(stats.isSymbolicLink()) {
        var linkstr = fs.readlink(config.base+"/cdn"+req.url);
        log.info("Redirecting request \""+req.url+"\" to \""+linkstr);
        var data = fs.readFile(linkstr);
        res.writeHead(200,{"Content-type":"text/html"});
        res.end(data);
      }
    });

On Monday, 22 September 2014 05:18:40 UTC+4, Matt Sergeant wrote:
>
>
> On Sat, Sep 20, 2014 at 9:17 AM, Bruno Jouhier <bjou...@gmail.com 
> <javascript:>> wrote:
>
>> How do you implement it with async?
>
>
> This is a good question, so here's an example translation (not the only 
> way to do it):
>
>
>     CDN.use(config.CDN.baseUrl, function(req, res, next) {
>         if(err) return next(err);
>         async.waterfall([
>             function (cb) {
>                 fs.lstat(config.base+"/cdn"+req.url, cb);
>             },
>             function (stats, cb) {
>                 if(stats.isSymbolicLink()) {
>                     async.waterfall([
>                         function (cb) {
>                             fs.readlink(config.base+"/cdn"+req.url, cb);
>                         },
>                         function (linkstr, cb) {
>                             log.info("Redirecting request \""+req.url+"\" 
> to \""+linkstr);
>                             fs.readFile(linkstr, cb);
>                         },
>                     ], cb);
>                 }
>             }
>         ], function (err, data) {
>             if (err) return next(err);
>             res.writeHead(200,{"Content-type":"text/html"});
>             res.end(data);
>         });
>     });
>
> It's more code, but it improves the error checking. If it didn't have that 
> "if" statement in there it would be a lot simpler - async code doesn't deal 
> well with "if" statements I've found (there's no way to short-circuit).
>
> Matt.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/36905e32-1293-4ea3-b2d8-7015509afa31%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to