简体   繁体   English

模块返回空数组

[英]Module returns empty array

I have a simple module return which makes a bunch of API calls to retrieve data and then return it我有一个简单的模块返回,它进行一堆 API 调用来检索数据然后返回它

export default async function getCandles(coins) {
  let data = [];
  _.forEach(coins, async coin => {
    let candles1h = await client.candles({
      symbol: coin,
      interval: "1h",
      limit: 500
    });
    let candles2h = await client.candles({
      symbol: coin,
      interval: "2h",
      limit: 500
    });
    let candles4h = await client.candles({
      symbol: coin,
      interval: "4h",
      limit: 500
    });
    let candles1d = await client.candles({
      symbol: coin,
      interval: "1d",
      limit: 500
    });

    let candles = {
      "1h": candles1h,
      "2h": candles2h,
      "4h": candles4h,
      "1d": candles1d
    };

    data.push({ coin: candles });
  });

  return data;
}

And I've got a test file which inputs the array parameter and sends it我有一个测试文件,它输入数组参数并发送它

async function test() {
  let candles = await getCandles(coins);

  console.log(candles);
}

When I call the test function, the output is just an empty array.当我调用测试函数时,输出只是一个空数组。 What am I doing wrong here?我在这里做错了什么?

The problem is that getCandles() has no way of knowing when each of the batches of async functions lodash.forEach() creates resolves.问题是getCandles()无法知道lodash.forEach()创建的每批异步函数lodash.forEach()解析。

One solution might be to call your batches of async functions using Promise.all() .一种解决方案可能是使用Promise.all()调用一批异步函数。 Promise.all() is await able and enables multiple calls to be sent simultaneously, rather than sequentially. Promise.all()可以await并允许同时发送多个调用,而不是按顺序发送。

See below for a rough example.请参阅下面的粗略示例。

// Get Candles.
const getCandles = async symbol => {
  const intervals = ['1h', '2h', '4h', '1d']
  const limit = 500
  const candles = await Promise.all(intervals.map(async interval => await client.candles({symbol, interval, limit})))
  return candles.reduce(async (c, x, i) => {
    c[intervals[i]] = x
    return c
  }, {})
}

// Get Coins.
const getCoins = async symbols => {
  const coins = symbols.map(async symbol => ({coin: await getCandles(symbol)}))
  return await Promise.all(coins)
}

// Test.
const test = async () => await getCoins(symbols)

I think I will do this.我想我会这样做。 But I'm not sure I have not tried it.但我不确定我没有尝试过。

export default async function getCandles(coins) {
  return await _.map(coins, async coin => {
    let candles1h = await client.candles({
      symbol: coin,
      interval: "1h",
      limit: 500
    });
    let candles2h = await client.candles({
      symbol: coin,
      interval: "2h",
      limit: 500
    });
    let candles4h = await client.candles({
      symbol: coin,
      interval: "4h",
      limit: 500
    });
    let candles1d = await client.candles({
      symbol: coin,
      interval: "1d",
      limit: 500
    });

    return {
      coin: {
        "1h": candles1h,
        "2h": candles2h,
        "4h": candles4h,
        "1d": candles1d
      }
    };
  });
}

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

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