Javascript:返回一个值到回调函数之外的变量 [英] Javascript: return a value to a variable outside of a callback function

查看:99
本文介绍了Javascript:返回一个值到回调函数之外的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常具体的问题,希望有人可以借给我一只手。对于NodeJS来说,当涉及到Javascript时,我还是很绿色的。

I have a very specific question and hopefully someone can lend me a hand here. I'm quite green when it comes to Javascript much more when it comes to NodeJS.

我使用 lodash _。forIn 遍历和添加到数组中的对象的功能。整个事情看起来像这样:

I am using lodash's _.forIn functionality to traverse and add into an object that's inside an array. The whole thing looks something like this:

[ 
  { id: 20,
    keywords: 'shirts'
  },
  { id: 18,
    keywords: 'shoes'
  } 
]

基本上,它是保存在db elasticsearch中的关键字列表。我试图找到一种方法来添加到这个对象一个计数器这些关键字返回的结果数量。

Basically it's list of keywords saved in in the db elasticsearch. I'm trying to find a way to add into this object a counter of the number of results these keywords would return.

这个计数将来自一个elasticsearch设置,我们使用我们的产品数据库搜索。

This count will come from an elasticsearch setup that we use to search our product database with.

我想得到的输出将是这样:

The output I'd like to get would be something like this:

[ 
  { id: 20,
    keywords: 'shirts',
    count: 4
  },
  { id: 18,
    keywords: 'shoes',
    count: 15
  } 
]

这是我现在的代码:

function get_list( data ) {

  var list_data = data;
  var list = _.forIn( data, function( item, n, list_data ) {
    get_count( item, function( count_number ) {
      list_data[n].count = count_number;
    })
  });
  console.log( list, list_data );
}

function get_count( item, callback ) {

  var query = { // elasticsearch query here };  

  // elasticsearch query here, that returns the count in a callback
  es.count( query, function(count_number) { callback(count_number) } );
} 



当我运行这个时,控制台会显示一个不变的数组。我检查了每个函数返回的所有数据,一切检查。我似乎不能在列表 list_data 上获取数据。

PS。
这不是我的实际代码,但它是它的要点,所以可能有一些错别字。

PS. This isn't my actual code but it's pretty much the gist of it, so there may be some typos.

此外,列表 list_data 是我试图找出这是如何工作的结果。我已经尝试阅读JS回调如何工作,但没有看到似乎能帮助我解决我的问题。

Also, list and list_data is a result of me trying to figure out how this works. I've tried reading up on how JS callbacks work but nothing I've seen seem to be able to help me with my problem.

推荐答案

您可以在所有回调完成后检查 list_data 对象。

You can check the list_data object only after all the callbacks have finished.

如果你想验证你的 get_count 函数是否正常工作,你可以移动console.log在 callback ,这将导致控制台中 list_data 的多个日志。

If you want to validate that your get_count function is working you can move the console.log inside the callback, this will result in multiple logs of the list_data in the console.

function get_list( data ) {

  var list_data = data;
  var list = _.forIn( data, function( item, n, list_data ) {
    get_count( item, function( count_number ) {
      list_data[n].count = count_number;
      console.log( list, list_data );
    })
  });

}

function get_count( item, callback ) {

  var query = { // elasticsearch query here };  

  // elasticsearch query here, that returns the count in a callback
  es.count( query, function(count_number) { callback(count_number) } );
} 


$ b $ p

为此,我建议使用 async

In order to do this i recommend using the async module with is pretty good to handle asynchronous calls like yours.

这是模块链接,所以你可以开始使用它: https://github.com/caolan/async

Here is the module link so you can start using it: https://github.com/caolan/async

这里是我如何实现 async 模块在我们的例子:

Here is how i would implement the async module in our case:

var async = require('async');

function get_list(data) {

    var list_data = data;
    //Transform all the list_data objects adding the count prop.
    async.map(list_data, get_count, function(err, results) {
        console.log(results);
    });
}

function get_count(item, callback) {
    var query = { // elasticsearch query here 
    };
    // elasticsearch query here, that returns the count in a callback
    es.count(query, function(count_number) {
        //add the new prop to the item
        item.count = count_number;
        //return the transformed object
        callback(null, item);
    });
}

这篇关于Javascript:返回一个值到回调函数之外的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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