Javascript 多阶段自动完成与nodejs&;redis呼叫。如何确保一切完成后返回

Javascript 多阶段自动完成与nodejs&;redis呼叫。如何确保一切完成后返回,javascript,node.js,redis,node-redis,Javascript,Node.js,Redis,Node Redis,我正在做一个自动完成的原型。我已经看了网上几个自动完成简单列表的例子。我的商业案例是多步骤的,因为auto complete必须处理同名但出生年份不同的人 范例 用户类型:Geor 为可能的完成返回: 乔治1976-父亲:老乔治母亲:凯伦 乔治1980年-父亲:杰克母亲:帕姆 乔治安娜1972年-父亲:大卫母亲:卡罗尔 因此,我有四个步骤来调用redis: zrank前缀“GEOR” >四, zrange前缀4 20 >格奥尔格 >乔治* >格奥尔基 >乔治亚州 >格鲁吉亚人 >乔治

我正在做一个自动完成的原型。我已经看了网上几个自动完成简单列表的例子。我的商业案例是多步骤的,因为auto complete必须处理同名但出生年份不同的人

范例

用户类型:Geor

为可能的完成返回:

  • 乔治1976-父亲:老乔治母亲:凯伦
  • 乔治1980年-父亲:杰克母亲:帕姆
  • 乔治安娜1972年-父亲:大卫母亲:卡罗尔
因此,我有四个步骤来调用redis:

  • zrank前缀“GEOR”
    • >四,
  • zrange前缀4 20
    • >格奥尔格
    • >乔治*
    • >格奥尔基
    • >乔治亚州
    • >格鲁吉亚人
    • >乔治亚娜*
  • (对于以*结尾的每个名称)
    • 记忆“康普南:乔治”
      • >“人名:10”
      • >“人名:15”
    • 成员“康普南:乔治亚娜”
      • >“人名:53”
  • (每人)
    • hgetall'人名:10'
    • hgetall'人名:15'
    • hgetall'人名:53'
希望这足够清楚

我正在尝试创建类似以下内容的输出:

 [ { name: 'George', yob: '1976', parentstr: 'Father: George Sr. Mother: Karen'},
 { name: 'George', yob: '1980', parentstr: 'Father: Jack Mother: Pam'},
 { name: 'Georgiana', yob: '1972', parentstr: 'Father: David Mother: Carol'}]
这是我的nodejs代码

var app = express();
var client = redis.createClient();

app.get('/names/:name', function(req,res) {
  var name_search = req.params.name;

  client.zrank('prefix',name_search, function (err, obj){
    client.zrange('prefix',obj+1,20, function(err,obj){
      var returnNames = [];
      async.each(obj,function(item){
        if(item.slice(item.length-1)==='*'){
          client.smembers('compnam:'+item.slice(0,item.length-1),function(err,obj){
            async.each(obj,function(item){
              client.hgetall(item,function(err,obj) {
                Array.prototype.push.call(returnNames,obj);
                console.log(returnNames);
              })
            },
            function(err) {
              console.log("error with redis1:"+err);
            });
          });
        }
      }, function(err) {
           console.log("error with redis2");
      });
      console.log("Executed here before names added");
    });
  });
});
我对Node.js还是很陌生,所以我意识到我的代码没有针对Node.js的异步特性进行正确的结构化。任何帮助都将不胜感激


-编辑-抱歉不够清晰。是的,console.log(“在添加名称之前在此处执行”)是在其他工作完成之前执行的。最终,它将更改为res.send(returnNames)。我需要一些帮助来重新构造代码,以便在最后一个console.log之前完成所有工作,或者如果我需要将最后一个console.log放在另一个回调中。

您可以使用一个promises,一些伪代码:

doSomethingAsynch()。然后(
函数(数据){//onfultill(异步完成时)
doSometingElseAsynch();
},

函数(错误){//我读了你的帖子,但没有看到有人问任何问题。@thtsigma我认为OP希望确保在一系列异步调用之后发生(返回)一些事情,所以我建议使用承诺。
doSomethingAsynch().then(
  function(data){      //onfultill (when the asynch is done)
    doSometingElseAsynch();
  },
  function(error){//<--onreject this is optional
  }
 ).then(....
doSomethingAsynch(//<--error here "Illegal input"
  function(data){
   anotherAsynchThing();//<--this one isn't called because there is an error
  } //<-- no reject method specified
 ).then(
   function(data){//<--this one not called there WAS an error, (it propagates)
   anotherAsynchThing();
  } 
 ).then(
   function(data){//<--not called
   anotherAsynchThing();
  } 
 ).then(
   function(data){<--not called
   anotherAsynchThing();
  },
  function(error){//<--this one called with error"Illegal input"
    //handle error
  }
);