Waiting will not work even in an asynchronous function

I googled this, but cannot find a result that relates to my problem. I put "wait" in the async function, but node.js says "SyntaxError: Unexpected identifier". Can anyone help? I recently started learning JavaScript.

async function removeFile(data) {
    return new Promise((resolve, reject) => {
        try {
            if (!data.arg) {
                //check if there 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 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 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 no repo channel set!");
                    });
                } else {
                    //insert good stuff here (Personal)
                    data.msg.channel.send("This command is WIP!");
                }
            }
        } catch (err) {
            reject(err)
        }
        resolve(true);
    });
}

edit: here is the content of getMessages () getMessages () is used to receive messages in the channel.

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);
    });
}
+4
source share
1 answer

You can awaitonly use inside async function. And you do not need to build Promise, that’s what async functionit does for you internally. In addition, the meaning awaitis to replace callbacks and then chains:

 async function removeFile(data) {
   if (!data.arg) {
     //check if there 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 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 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 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;
 }
+1

Source: https://habr.com/ru/post/1695930/


All Articles