用于遍历事件驱动code? [英] for loop over event driven code?

查看:160
本文介绍了用于遍历事件驱动code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Redis的数据存储我有钥匙的名单,我想遍历键的列表和Redis的获取这些值。美中不足的是我使用的是事件驱动的语言,通过Node.js的JavaScript的

如果JavaScript的是程序我能做到这一点。

 函数GETALL(回调){
    变种列表= redis.lrange(lrange('的myKey',0,-1);
    对于(VAR I = 0; I< List.length的数字,我+ = 1){
        名单[I] = redis.hgetall(名单[I]);
    }
    回调(名单);
}

但是,我不能,为此..我做到这一点?

 函数GETALL(回调){
    redis.lrange('的myKey',0,-1,函数(ERR,回复){
        //回复转换成信息
        VAR名单= [];
        变种指数= -1;
        VAR递归函数=(){
            如果(指数== reply.length){
                回调(名单);
            }其他{
                redis.hgetall(回复[I],功能(ERR,回复){
                    list.push(答复);
                    指数+ = 1;
                    递归();
                });
            }
        };
        递归()
    });
};

本缝错了,因为,而不是在一次执行的所有请求,然后又让回调插入到列表中,我强迫顺序调用序列。如果有钥匙的1000会发生什么?

我能做到这一点一些如何?

 函数GETALL(回调){
    redis.lrange('的myKey',0,-1,函数(ERR,回复){        //回复转换成信息
        VAR名单= [];
        VAR插入=功能(ERR,回复){
            list.push(答复);
        };
        对于(VAR I = 0; I< reply.length; I + = 1){
            redis.hgetall(回复[I],插入);
        }        ???如何阻止直到完成?
        回调(名单);
    });
};


解决方案

您内派遣您的来电为循环前声明对象变量。每个调用可以不是增加它的结果到对象。

然后,您需要code等待所有调用来完成。这可以帮助你: https://gist.github.com/464179

例如:

 函数GETALL(回调){    变种结果= [];    变种B =新的屏障(2,函数(){
        //全部完成回调
        回电话();
        },函数(){
        //中止回调,这里不使用
    });    变种列表= redis.lrange(lrange('的myKey',0,-1);
    对于(VAR I = 0; I< List.length的数字,我+ = 1){
        //派遣您的来电
        调用(函数(富){
            results.push(富);
            b.submit();
        });
    }
}

请注意,()调用应该是你的异步数据库功能,上执行结果回调。

In a redis datastore I have a list of keys, I want to iterate over that list of keys and get those values from redis. The catch is I am using an event driven language, javascript via node.js

If javascript were procedural I could do this

function getAll(callback) {
    var list = redis.lrange(lrange('mykey', 0, -1);
    for ( var i = 0; i < list.length; i+= 1 ) {
        list[i] = redis.hgetall(list[i]);
    }
    callback(list);
}

But, I cannot, therefor.. I do this?

function getAll(callback) {
    redis.lrange('mykey', 0, -1, function(err, reply) {
        // convert reply into messages
        var list = [];
        var index = -1;
        var recurse = function() {
            if ( index == reply.length ) {
                callback(list);
            } else {
                redis.hgetall(reply[i], function(err, reply) {
                    list.push(reply);
                    index += 1;
                    recurse();
                });
            }
        };
        recurse()
    });
};

This seams wrong, because, instead of executing all requests at once, and then letting callbacks insert onto list, I am forcing a sequential call sequence. What happens if there are 1000s of keys?

Could I do this some how?

function getAll(callback) {
    redis.lrange('mykey', 0, -1, function(err, reply) {

        // convert reply into messages
        var list = [];
        var insert = function(err, reply) {
            list.push(reply);
        };
        for ( var i = 0; i < reply.length; i += 1 ) {
            redis.hgetall(reply[i], insert);
        }

        ??? how to block until finished ??? 
        callback(list);
    });
};

解决方案

Declare an object variable before you dispatch your calls within the for loop. Each call can than add its result into the object.

You then need code to wait for all calls to be done. This might help you: https://gist.github.com/464179

Example:

function getAll(callback) {

    var results = [];

    var b = new Barrier(2, function() {
        // all complete callback
        callback();
        }, function() {
        // Aborted callback, not used here
    });

    var list = redis.lrange(lrange('mykey', 0, -1);
    for ( var i = 0; i < list.length; i+= 1 ) {
        //dispatch your call
        call(function(foo){
            results.push(foo);
            b.submit();
        });
    }
}

Please note that call() should be your async database function, that executes the callback on result.

这篇关于用于遍历事件驱动code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆