Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 对Redis db的异步NodeJS请求_Node.js_Redis - Fatal编程技术网

Node.js 对Redis db的异步NodeJS请求

Node.js 对Redis db的异步NodeJS请求,node.js,redis,Node.js,Redis,我需要对Redis db执行几个请求,我需要一个请求的响应来执行下一个请求 让我们分析一下这个例子: (我使用的是请求) 这种方法不起作用,因为调用client.hmdet()时消息id仍然未定义 下面是惰性方法,它包括将client.hmdet()放在client.incr()回调中。 var消息id=未定义 // request the next id client.incr("message-id", function(err, id) { message-id = id;

我需要对Redis db执行几个请求,我需要一个请求的响应来执行下一个请求

让我们分析一下这个例子: (我使用的是请求)

这种方法不起作用,因为调用
client.hmdet()
时消息id仍然未定义

下面是惰性方法,它包括将
client.hmdet()
放在
client.incr()
回调中。 var消息id=未定义

// request the next id
client.incr("message-id", function(err, id) {
    message-id = id;

    // use the 'next id' for storing a message
    client.hmset("messages", message-id, messageContent, function(){
        //more request here
    });
});
我认为这不是一个好的继续方式,因为如果我有很多链式请求,我需要将每个请求放在它以前的请求回调中,这将创建一个难以维护的代码块


我对异步执行大量请求的最佳实践感兴趣。有人能给我一些建议吗?

我建议您看看,它实现了几种您可以使用的模式。 您的示例是异步瀑布

async.waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done'    
});

此外,如果您想探索回调模式之外的其他可能性,您可以在第一个代码块的第6行中实现

your'r missing a closing)(谢谢!Q模块真的很有趣!无论如何,我认为如果任何redis方法都有一个将请求设置为sync或async的标志,那么这将非常有用…;)我真的不明白如何用一种+的方式来实现承诺。。。在我看来,我解释得不好…——”
async.waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done'    
});