【问题标题】:'await' won't work even in an async function'await' 即使在异步函数中也不起作用
【发布时间】:2018-09-17 20:38:21
【问题描述】:

我用谷歌搜索了这个,但找不到与我的问题相关的结果。我将“等待”放在异步函数中,但 node.js 说“SyntaxError:意外标识符”。有人可以帮忙吗?我最近才开始学习 JavaScript。

async function removeFile(data) {
    return new Promise((resolve, reject) => {
        try {
            if (!data.arg) {
                //check if there's filename
                data.msg.channel.send("What did you want me to remove baka?").then(async result => {
                    data.client.setTimeout(data.tsundere, 5000, result);
                });
            } else {
                //check if there's repo id in the config file (Personal)
                if (!data.config.group && !data.config.repo_id) {
                    data.msg.channel.send("Hmmph! Please set the repo channel ID in the config file first?");
                    //check if the channel is valid (Personal)
                } else if (!data.config.group && !data.msg.guild.channels.has(data.config.repo_id)) {
                    data.msg.channel.send("You just wasted my time finding an inexistent channel!");
                    //check if the repo channel is set through the repo command
                } else if (data.config.group) {
                    data.shimautils.sdataCheck(data.sdata, data.msg.guild.id).then(onRes => {
                        if (onRes.length < 1) {
                            data.msg.channel.send("There's no repo channel set!");
                        } else {
                            //insert good stuff here
                            data.msg.channel.send("This command is WIP!");
                            let gch = data.msg.guild.channels.get(data.sdata.get(data.msg.guild.id)[0]),
                                temp;
                            //the problem lies here
                            await getMessages(data.msg.guild, data.msg.channel);
                            console.log(temp);
                            data.msg.channel.send(temp.size);
                        }
                    }, async () => {
                        data.msg.channel.send("There's no repo channel set!");
                    });
                } else {
                    //insert good stuff here (Personal)
                    data.msg.channel.send("This command is WIP!");
                }
            }
        } catch (err) {
            reject(err)
        }
        resolve(true);
    });
}

编辑:这里是getMessages()的内容 getMessages() 用于获取频道中的消息。

async function getMessages(guild, channel) {
    return new Promise(async (resolve, reject) => {
        try {
            if (!channel.id) {
                reject(false);
            } else if (!guild.channels.has(channel.id)) {
                reject(false);
            } else {
                var fetchedMessages, fetchedSize, plscontinue = true,
                    firsttime = true;
                channel.fetchMessages({
                    'limit': 100
                }).then(async result => {
                    fetchedMessages = result.clone();
                }, async rej => {
                    reject(rej);
                });
                while (plscontinue) {
                    if (firsttime) {
                        fetchedSize = fetchedMessages.size;
                        firsttime = false;
                    }
                    if (fetchedSize == 100) {
                        plscontinue = true;
                        channel.fetchMessages({
                            'limit': 100,
                            'before': fetchedMessages.lastKey()
                        }).then(async fetched2 => {
                            fetchedSize = fetched2.size;
                            fetchedMessages = fetchedMessages.concat(fetchedMessages, fetched2)
                        }, async err => reject(err));
                    } else {
                        plscontinue = false;
                    }
                }
            }
        } catch (err) {
            reject(err);
        }
        resolve(fetchedMessages);
    });
}

【问题讨论】:

  • 你在等待函数中使用new Promise吗?嗯...您能否提供更多信息(关于节点中的错误)?
  • 哦,不。显式 Promise 构造函数反模式。 stackoverflow.com/questions/23803743/…

标签: javascript promise async-await discord.js


【解决方案1】:

你只能在异步函数中使用await直接。而且您不需要构造 Promise,这就是 async function 在内部为您所做的事情。此外,await 的意义是替换回调然后链接:

 async function removeFile(data) {
   if (!data.arg) {
     //check if there's filename
     const result = await data.msg.channel.send("What did you want me to remove baka?")
     data.client.setTimeout(data.tsundere, 5000, result);           
    } else {
        //check if there's repo id in the config file (Personal)
        if (!data.config.group && !data.config.repo_id) {
            await data.msg.channel.send("Hmmph! Please set the repo channel ID in the config file first?");
            //check if the channel is valid (Personal)
        } else if (!data.config.group && !data.msg.guild.channels.has(data.config.repo_id)) {
            await data.msg.channel.send("You just wasted my time finding an inexistent channel!");
            //check if the repo channel is set through the repo command
        } else if (data.config.group) {
          const onRes = await data.shimautils.sdataCheck(data.sdata, data.msg.guild.id);
          if (onRes.length < 1) {
            await data.msg.channel.send("There's no repo channel set!");
           } else {
             //insert good stuff here
             await data.msg.channel.send("This command is WIP!");
             let gch = data.msg.guild.channels.get( data.sdata.get(data.msg.guild.id)[0]);

             const temp = await getMessages(data.msg.guild, data.msg.channel);
             await data.msg.channel.send(temp.size);
          }

          await data.msg.channel.send("There's no repo channel set!");

        } else {
          //insert good stuff here (Personal)
          await data.msg.channel.send("This command is WIP!");
       }
   }
   return true;
 }

这同样适用于getMessage

 async function getMessages(guild, channel) {
    if (!channel.id) {
      throw false; //???
    } else if (!guild.channels.has(channel.id)) {
      throw false; //???
    }

    let fetchedMessages = await channel.fetchMessages({ 'limit': 100 });        
    const result = fetchedMessages.clone();            

    while (fetchedMessages.length >= 100) {
         fetchedMessages = await channel.fetchMessages({
              limit: 100,
              before: fetchedMessages.lastKey()
         });
        result.push(...fetchedMessages);
    }
    return result;
 }

【讨论】:

  • 我有一个问题。 fetchedMessages 之前的三个点 (...) 是什么意思?
  • @takase 它的 扩展运算符result.push(...[1,2,3])result.push(1,2,3) 相同。因此这一行会将fetchedMessages 附加到result>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 2020-12-23
  • 2021-04-18
  • 2017-02-02
  • 1970-01-01
相关资源
最近更新 更多