对于在循环使用Redis的异步​​的NodeJS请求 [英] For loop in redis with nodejs asynchronous requests

查看:408
本文介绍了对于在循环使用Redis的异步​​的NodeJS请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,Redis的和的NodeJS。我不得不遍历的电话号码列表,并检查是否这个数字是我的Redis数据库present。这里是我的code:

I've got a problem with redis and nodejs. I have to loop through a list of phone numbers, and check if this number is present in my redis database. Here is my code :

function getContactList(contacts, callback) {
  var contactList = {};

  for(var i = 0; i < contacts.length; i++) {
    var phoneNumber = contacts[i];

    if(utils.isValidNumber(phoneNumber)) {

      db.client().get(phoneNumber).then(function(reply) {
        console.log("before");
        contactList[phoneNumber] = reply;
      });
    }
  }

  console.log("after");
  callback(contactList);

};

本经控制台登录控制台之前日志之前出现,并且回调总是返回一个空 contactList 。这是因为请求的Redis是异步的,如果我深知。但事情是我不知道如何使它工作。
我该怎么办?

The "after" console log appears before the "before" console log, and the callback always return an empty contactList. This is because requests to redis are asynchronous if I understood well. But the thing is I don't know how to make it works. How can I do ?

推荐答案

您有两个主要问题。


  1. phoneNumber的变量不会是你希望它是什么。这可以通过改变你的阵列,因为意志的 .forEach() .MAP()迭代固定创建当前变量局部函数范围内。

  1. Your phoneNumber variable will not be what you want it to be. That can be fixed by changing to a .forEach() or .map() iteration of your array because that will create a local function scope for the current variable.

您必须创建一个办法知道,当所有的异步操作完成。有很多重复的问题/答案显示如何做到这一点。你可能想使用 Promise.all()

You have create a way to know when all the async operations are done. There are lots of duplicate questions/answers that show how to do that. You probably want to use Promise.all().

我建议这个解决方案,它利用的承诺你已经有:

I'd suggest this solution that leverages the promises you already have:

function getContactList(contacts) {
    var contactList = {};
    return Promise.all(contacts.filter(utils.isValidNumber).map(function(phoneNumber) {
        return db.client().get(phoneNumber).then(function(reply) {
            // build custom object
            constactList[phoneNumber] = reply;
        });
    })).then(function() {
        // make contactList be the resolve value
        return contactList;
    });
}

getContactList.then(function(contactList) {
    // use the contactList here
}, funtion(err) {
    // process errors here
});

下面是如何工作的:


  1. 呼叫 contacts.filter(utils.isValidNumber)来阵列筛选,唯一有效的数字。

  2. 呼叫 .MAP()通过过滤阵列来迭代

  3. 返回db.client()获得(phoneNumber的) .MAP()回调创建承诺的数组。

  4. 获取数据的电话号码之后,该数据添加到您的自定义 contactList 对象(这是本质上是 .MAP的副作用()循环。

  5. 使用 Promise.all()承诺返回数组就知道,当他们都做。

  6. 请我们建立了为返回的承诺的决心值 contactList 对象。

  7. 然后,调用它只是使用与。然后()返回的承诺,得到最终结果。无需添加一个回调参数,当你已经有一个承诺,你可以返回。

  1. Call contacts.filter(utils.isValidNumber) to filter the array to only valid numbers.
  2. Call .map() to iterate through that filtered array
  3. return db.client().get(phoneNumber) from the .map() callback to create an array of promises.
  4. After getting the data for the phone number, add that data to your custom contactList object (this is essentially a side effect of the .map() loop.
  5. Use Promise.all() on the returned array of promises to know when they are all done.
  6. Make the contactList object we built up be the resolve value of the returned promise.
  7. Then, to call it just use the returned promise with .then() to get the final result. No need to add a callback argument when you already have a promise that you can just return.

这篇关于对于在循环使用Redis的异步​​的NodeJS请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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