Javascript 循环中出现意外的“wait”。(循环中无等待)

Javascript 循环中出现意外的“wait”。(循环中无等待),javascript,async-await,Javascript,Async Await,我应该如何在循环内部等待bot.sendMessage() 也许我需要等待承诺。所有的但是我不知道如何添加到bot.sendMessage() 代码: const promise = query.exec(); promise.then(async (doc) => { let count = 0; for (const val of Object.values(doc)) { ++count; await bot.sendMessage(m

我应该如何在循环内部等待
bot.sendMessage()
也许我需要
等待承诺。所有的
但是我不知道如何添加到
bot.sendMessage()

代码:

const promise = query.exec();
promise.then(async (doc) => {
    let count = 0;
    for (const val of Object.values(doc)) {
        ++count;
        await bot.sendMessage(msg.chat.id, `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, `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, `I had a similar issue recently, I was getting lintting errors when trying to run an array of functions in a chain as apposed to asynchronously.

and this worked for me...

const myChainFunction = async (myArrayOfFunctions) => {
  let result = Promise.resolve()
  myArrayOfFunctions.forEach((myFunct) => {
    result = result.then(() => myFunct()
  })
  return result
}
const promise=query.exec();
承诺。然后(异步(doc)=>{
让计数=0;
for(对象值的常量值(doc)){
++计数;

等待bot.sendMessage(msg.chat.id,`如果您需要一次发送一条消息,那么您所拥有的一切都很好,您可以忽略eslint错误,如下所示:

const promise=query.exec();
然后(异步文档=>{
/*eslint禁用循环中的无等待*/
for(Object.values(doc.entries())的常量[index,val]{
常数计数=索引+1;

wait bot.sendMessage(msg.chat.id,`执行
wait
内部循环可以避免迭代在大多数情况下不具有依赖性,这就是为什么
eslint
警告它

您可以将代码重写为:

const promise=query.exec();
承诺。然后(异步(doc)=>{

wait Promise.all(Object.values(doc.map)((val,idx)=>bot.sendMessage(msg.chat.id,`最近我遇到了一个类似的问题,我在尝试异步运行一个链中的函数数组时遇到了lintting错误

这对我很有用

const myChainFunction=async(myarrayofffunctions)=>{
让结果=Promise.resolve()
MyArrayOfffunctions.forEach((myFunct)=>{
结果=结果。然后(()=>myFunct()
})
返回结果

}
消息需要一次发送一条还是一次发送所有消息?使用此代码,我会在消息1之后发送消息2…但是消息没有排序。您还没有回答问题。我不明白,但我想一条一条地发送消息。为什么不使用
承诺。所有
等待承诺。全部(Object.values(doc).map((val,idx)=>bot.sendMessage(msg.chat.id,`我想使用此代码获取用户消息并向他们显示这些消息,这种方式适合我的问题吗?@SedricHeidarizarei第一个块将使您获得的eslint错误保持沉默。第二个块在功能上并不等效,但如果它对您有效,那么这可能更可取,因为它更高效。