控制节点/ redis和回调的流量问题? [英] Control flow issue with node/redis and callbacks?

查看:68
本文介绍了控制节点/ redis和回调的流量问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问有关node和redis的控制流问题的一些建议吗? (也就是Python编码器试图习惯JavaScript)

Please could I ask for some advice on a control flow issue with node and redis? (aka Python coder trying to get used to JavaScript)

我不明白为什么 client.smembers client.get (Redis查找)需要回调而不仅仅是语句 - 它会让生活变得非常复杂。

I don't understand why client.smembers and client.get (Redis lookups) need to be callbacks rather than simply being statements - it makes life very complicated.

基本上我想查询一个集合,然后当我得到集合的结果时,我需要为每个结果执行一个get。 当我获得所有数据时,我需要将其广播回客户端。

Basically I'd like to query a set, and then when I have the results for the set, I need to carry out a get for each result. When I've got all the data, I need to broadcast it back to the client.

目前我在两个回调中使用全局对象执行此操作,这看起来很混乱。我甚至不确定它是否安全(代码是否会在启动另一个之前等待一个 client.get 完成?)。

Currently I do this inside two callbacks, using a global object, which seems messy. I'm not even sure if it's safe (will the code wait for one client.get to complete before starting another?).

当前代码如下所示:

var all_users = [];
// Get all the users for this page.
client.smembers("page:" + current_page_id, function (err, user_ids ) {
  // Now get the name of each of those users.
  for (var i = 0; i < user_ids.length; i++) {
     client.get('user:' + user_ids[i] + ':name', function(err, name) {
       var myobj = {};
       myobj[user_ids[i]] = name;
       all_users.push(myobj);  
       // Broadcast when we have got to the end of the loop, 
       // so all users have been added to the list - 
       // is this the best way? It seems messy.  
       if (i === (user_ids.length - 1)) {
           socket.broadcast('all_users', all_users); 
       }
     });       
   }
 });

但这看起来非常混乱。这真的是最好的方法吗?在调用 socket.broadcast 之前,如何确保所有查找都已执行?

But this seems very messy. Is it really the best way to do this? How can I be sure that all lookups have been performed before calling socket.broadcast?

划痕头提前感谢任何建议。

推荐答案


我不明白为什么 client.smembers client.get (Redis查找)需要回调而不仅仅是语句 - 它让生活变得非常复杂。

I don't understand why client.smembers and client.get (Redis lookups) need to be callbacks rather than simply being statements - it makes life very complicated.

这就是Node。 (我很确定这个话题在这里已经讨论了很多次,看看其他问题,肯定在那里)

That's what Node is. (I'm pretty sure that this topic was discussed more than enough times here, look through other questions, it's definitely there)


怎么能我确定在调用 socket.broadcast 之前已经执行了所有查找?

How can I be sure that all lookups have been performed before calling socket.broadcast?

在回调函数中,这就是 err 。这是Node的标准 - 回调中的第一个参数是错误对象( null 如果一切正常)。所以只需使用这样的东西以确保没有错误发生:

That's what is err for in callback function. This is kinda Node's standard - first parameter in callback is error object (null if everything fine). So just use something like this to be sure no errors occurred:

if (err) {
  ...    // handle errors.
  return // or not, it depends.
}

... // process results




但这看起来非常混乱。

But this seems very messy.

你会习惯的。当代码格式正确且项目结构巧妙时,我实际上发现它很好。

You'll get used to it. I'm actually finding it nice, when code is well formatted and project is cleverly structured.

其他方式是:


  • 使用库来控制异步代码流( Async.js Step.js 等。)

  • 如果是意大利面条式代码是你认为混乱是什么,定义一些函数来处理结果并将它们作为参数传递而不是匿名的。

  • Using libraries to control async code-flow (Async.js, Step.js, etc.)
  • If spaghetti-style code is what you think mess is, define some functions to process results and pass them as parameters instead of anonymous ones.

这篇关于控制节点/ redis和回调的流量问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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