简体   繁体   English

Node.js - Async.js 映射函数仅显示上次迭代的结果?

[英]Node.js - Async.js map function only showing results of the last iteration?

Sorry for the title, but I can't think of an easier way to word this.对不起,标题,但我想不出更简单的方法来表达这个。

I'm trying to use the async library with Node.js to run 3 asynchronous functions and do some operation on the results of all 3. From what I understand, the map() function allows me to run all 3 and apply an argument to an iterator, then provide a callback to operate on the results of all 3 async functions (stored in an array).我正在尝试使用带有 Node.js 的异步库来运行 3 个异步函数并对所有 3 个的结果进行一些操作。据我所知,map() 函数允许我运行所有 3 个并将参数应用于一个迭代器,然后提供一个回调来操作所有 3 个异步函数的结果(存储在一个数组中)。

The results are showing 3 successful responses, but they all contain the data from that last (3rd) function run, the other 2 are overwritten for some reason.结果显示 3 个成功响应,但它们都包含最后(第 3 个)函数运行的数据,其他 2 个由于某种原因被覆盖。

For example,例如,

/**map the array of exchange names through the iterator function
 * xchange is an external library which has functions of the form -
 * xchange.bitstamp.ticker(callback), xchange.bitfinex.ticker(callback), etc..
 */
var xchange = require("xchange.js"),
    async = require("async");

async.map([ "bitfinex", "bitstamp", "okcoin" ], 
function(item, callback) {
    xchange[item].ticker(function(err, body) {
        console.log("item: " + item);
        console.log(body);

        return callback(err, body);
    });
}, function(err, results) {
    console.log(results);
});


//print out
item: bitstamp
{ bid: 275.16,
  ask: 275.21,
  low: 245,
  high: 309.9,
  volume: 54017.1092978,
  timestamp: 1422283998 }
item: okcoin
{ bid: 279.25,
  ask: 280.44,
  low: 242.93,
  high: 310.57,
  volume: 29342.543,
  timestamp: 1422284015 }
item: bitfinex
{ bid: 279.2,
  ask: 279.77,
  low: 246.59,
  high: 315,
  volume: 165380.17021898,
  timestamp: 1422284011.1361156 }

//Why are these all from the last run?  Should be the results of all 3 above
[ { bid: 279.2,
    ask: 279.77,
    low: 246.59,
    high: 315,
    volume: 165380.17021898,
    timestamp: 1422284011.1361156 },
  { bid: 279.2,
    ask: 279.77,
    low: 246.59,
    high: 315,
    volume: 165380.17021898,
    timestamp: 1422284011.1361156 },
  { bid: 279.2,
    ask: 279.77,
    low: 246.59,
    high: 315,
    volume: 165380.17021898,
    timestamp: 1422284011.1361156 } ]

Basically the xchange ticker function uses the request module to call an http request on each ticker api.基本上,xchange 股票代码函数使用请求模块在每个股票代码 api 上调用 http 请求。 The results (somewhat transformed) are returned.结果(有些转换)被返回。

I'm able to run the following code with no errors, so I'm certain it has something to do with my xchange library code.我能够无错误地运行以下代码,所以我确定它与我的 xchange 库代码有关。

Any tips or ideas on what could cause this?关于可能导致这种情况的任何提示或想法?

I had a similar problem where it appeared that async map was rewriting the results to all the be same.我有一个类似的问题,似乎异步映射正在将结果重写为相同的结果。 I turns out it wasn't an async problem but a problem with what my function was returning as a result.我发现这不是异步问题,而是我的函数结果返回的问题。

My problem was that I was passing an object to the result, and passing the same object, but changing the values within it each time.我的问题是我将一个对象传递给结果,并传递相同的对象,但每次都更改其中的值。 This ended up meaning I had an array of several references to a single object.这最终意味着我有一个对单个对象的多个引用的数组。 So when I examined my result array it looked like it had returned the last object several times, when in reality I'd only returned one object several times and changed the values in it in each loop.因此,当我检查我的结果数组时,它看起来好像多次返回最后一个对象,而实际上我只返回一个对象几次并在每个循环中更改其中的值。 I suspect you have the same issue.我怀疑你有同样的问题。

If in your library you create an object, let's call it bitCoin, and then you return that on the first map loop, then you use the same bitCoin object and change the values within it and return on the second map loop, then the values for every reference to that object will also be updated, which means the first result will now look like your second result.如果在您的库中创建了一个对象,我们称其为 bitCoin,然后在第一个 map 循环中返回它,然后使用相同的 bitCoin 对象并更改其中的值并在第二个 map 循环中返回,然后返回对该对象的每个引用也将被更新,这意味着第一个结果现在看起来像您的第二个结果。

正如您在响应中链接的那样,在callback引用相同的对象,使map函数在每次迭代中覆盖该对象,这使最终结果成为同一对象的数组,特别是最后一次迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM