The best pattern for handling async looping in Node.js

前端 未结 3 569
有刺的猬
有刺的猬 2021-01-03 00:01

I\'m new to Node and trying to ensure that I\'m using sane designs for a JSON-driven web app.

I\'ve got a bunch of data stored in Redis and am retrieving it through

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-03 00:46

    The above code might not do what you expect. You're kicking off each .get() in sequence, but they might not call back in sequence — so the results could stream out in any order. If you want to stream the results instead of collecting them in memory, you need to .get() in sequence.

    I think that caolan’s async library makes a lot of this easier. Here’s one way you could use it to get each item in sequence (warning, untested):

    app.get("/facility", function(req, res) {
        rc.keys("FACILITY*", function(err, replies) {
            var i = 0;
            res.write("[");
            async.forEachSeries(replies, function(reply, callback){
                rc.get(reply, function(err, reply) {
                    if (err){
                        callback(err);
                        return;
                    }
                    res.write(reply);
                    if (i < replies.length) {
                        res.write(",");
                    }
                    i++;
                    callback();
                });
            }, function(err){
                if (err) {
                    // Handle an error
                } else {
                    res.end(']');
                }
            });
        });
    });
    

    If you don’t care about the order, just use async.forEach() instead.

    If you wouldn’t mind collecting the results and want them to return in sequence, you could use async.map() like this (warning, also untested):

    app.get("/facility", function(req, res) {
        rc.keys("FACILITY*", function(err, replies) {
            async.map(replies, rc.get.bind(rc), function(err, replies){
                if (err) {
                    // Handle an error
                } else {
                    res.end('[' + replies.join(',') + ']');
                }
            });
        });
    });
    

提交回复
热议问题