Unexpected `await` inside a loop. (no-await-in-loop)

后端 未结 2 997
生来不讨喜
生来不讨喜 2020-12-09 14:40

How Should I await for bot.sendMessage() inside of loop?
Maybe I Need await Promise.all But I Don\'t Know How Should I add to bot.sendM

相关标签:
2条回答
  • 2020-12-09 15:17

    If you need to send each message one-at-a-time, then what you have is fine, and according to the docs, you can just ignore the eslint error like this:

    const promise = query.exec();
    promise.then(async doc => {
      /* eslint-disable no-await-in-loop */
      for (const [index, val] of Object.values(doc).entries()) {
        const count = index + 1;
        await bot.sendMessage(msg.chat.id, `                                                                    
    0 讨论(0)
  • 2020-12-09 15:22

    Performing await inside loops can be avoided once iterations doesn't have dependency in most cases, that's why eslint is warning it here

    You can rewrite your code as:

    const promise = query.exec();
      promise.then(async (doc) => {
        await Promise.all(Object.values(doc).map((val, idx) => bot.sendMessage(msg.chat.id, `                                                                    
    0 讨论(0)
提交回复
热议问题