javascript - How to call function after completion of async functions inside loop? - Stack Overflow

I have a forEach loop in NodeJS, iterating over a series of keys, the values of which are then retrieve

I have a forEach loop in NodeJS, iterating over a series of keys, the values of which are then retrieved asynchronously from Redis. Once the loop and retrieval has plete, I want to return that data set as a response.

My problem at the moment is because the data retrieval is asyncrhonous, my array isn't populated when the response is sent.

How can I use promises or callbacks with my forEach loop to make sure the response is sent WITH the data?

exports.awesomeThings = function(req, res) {
    var things = [];
    client.lrange("awesomeThings", 0, -1, function(err, awesomeThings) {
        awesomeThings.forEach(function(awesomeThing) {
            client.hgetall("awesomething:"+awesomeThing, function(err, thing) {
                things.push(thing);
            })
        })
        console.log(things);
        return res.send(JSON.stringify(things));
    })

I have a forEach loop in NodeJS, iterating over a series of keys, the values of which are then retrieved asynchronously from Redis. Once the loop and retrieval has plete, I want to return that data set as a response.

My problem at the moment is because the data retrieval is asyncrhonous, my array isn't populated when the response is sent.

How can I use promises or callbacks with my forEach loop to make sure the response is sent WITH the data?

exports.awesomeThings = function(req, res) {
    var things = [];
    client.lrange("awesomeThings", 0, -1, function(err, awesomeThings) {
        awesomeThings.forEach(function(awesomeThing) {
            client.hgetall("awesomething:"+awesomeThing, function(err, thing) {
                things.push(thing);
            })
        })
        console.log(things);
        return res.send(JSON.stringify(things));
    })
Share Improve this question asked Apr 1, 2014 at 14:42 AnonymousAnonymous 6,2517 gold badges46 silver badges72 bronze badges 3
  • There are many QA on this problem. Some are listed on the right of this page. – Denys Séguret Commented Apr 1, 2014 at 14:46
  • 1 Right now, I'd suggest to use a promise library. Here's an introduction – Denys Séguret Commented Apr 1, 2014 at 14:47
  • @dystroy you should consider adding a promise answer to that question. The current one is rather ugly. Also, something something asynchronous semaphore. – Benjamin Gruenbaum Commented Apr 1, 2014 at 14:53
Add a ment  | 

2 Answers 2

Reset to default 11

I use Bluebird promises here. Note how the intent of the code is rather clear and there is no nesting.

First, let's promisify the hgetall call and the client -

var client = Promise.promisifyAll(client);

Now, let's write the code with promises, .then instead of a node callback and aggregation with .map. What .then does is signal an async operation is plete. .map takes an array of things and maps them all to an async operation just like your hgetall call.

Note how Bluebird adds (by default) an Async suffix to promisifed methods.

exports.awesomeThings = function(req, res) {
    // make initial request, map the array - each element to a result
    return client.lrangeAsync("awesomeThings", 0, -1).map(function(awesomeThing) {
       return client.hgetallAsync("awesomething:" + awesomeThing);
    }).then(function(things){ // all results ready 
         console.log(things); // log them
         res.send(JSON.stringify(things)); // send them
         return things; // so you can use from outside
    });
};

No lib is needed. Easy as pie, it's just an async loop. Error handling is omitted. If you need to do a parallel async loop use a counter.

exports.awesomeThings = function(req, res) {
    client.lrange("awesomeThings", 0, -1, function(err, awesomeThings) {
        var len = awesomeThings.length;
        var things = [];
        (function again (i){
            if (i === len){
                //End
                res.send(JSON.stringify(things));
            }else{
                client.hgetall("awesomething:"+awesomeThings[i], function(err, thing) {
                    things.push(thing);

                    //Next item
                    again (i + 1);
                })
            }
        })(0);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744332582a4568959.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信