forEach不要等待我的等待功能

时间:2019-04-20 19:18:19

标签: javascript node.js asynchronous

请在下面查看我的代码。我想这样做,以便我的foreach循环将等待我评论过的结果。目前,我的循环正在执行:

1... 2... get results for getCurrencyExchangeRate(1)...

我需要这个: 1... get results for getCurrencyExchangeRate(1)... 2...

我在下面称这个方法

async function getCurrencyExchangeRate(currencyId) {
  try {
    const isCurrencyExchangeRate = await Currency.findOne({
      where: {
        id: currencyId,
      },
    });
    return isCurrencyExchangeRate.currency_exchange_rate;
  } catch (e) {
    console.log(e);
  }
}
//...

其他功能...

await [1, 2].forEach(async transactionHistory => {
  if (transactionHistory.id_recipient === userId) {
    //...
    else {
      transferCurrencyExchangeRate = await getCurrencyExchangeRate(
        transactionHistory.id_currency,
      ); // <== DONT WAITING FOR RESULT FOR 1 ELEMENT

      if (mainCurrency) {
        convertedAmountMoney =
          transactionHistory.amount_money /
          transferCurrencyExchangeRate;

        incomingTransfersSum += convertedAmountMoney;
        availableFunds += convertedAmountMoney;
        accountBalanceHistory += `,${availableFunds.toFixed(2)}`;
        console.log('accountBalanceHistory 6', accountBalanceHistory);
      }
    }
  }
});

我正在寻求帮助以达到这个结果。

0 个答案:

没有答案