On Sat, Sep 20, 2014 at 9:17 AM, Bruno Jouhier <[email protected]> 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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAPJ5V2Z8yeYWX_HXaJqvbid5K%2B1T%3DNz--VF0GtXnKr%2BsyZsQrA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to