【问题标题】:Javascript - error handling stopping codeJavascript - 错误处理停止代码
【发布时间】:2023-04-06 01:51:01
【问题描述】:

快速基本问题,

在使用 discord.js v13 设置 guildmember.timout 时,它给出了 .then(console.log).catch(console.error) 的示例。使用示例,代码将在 .catch 之后继续。

      muteMember
        .timeout(time, reason)
        .catch((error) => {
          return errors(client, message, args, "mute", error, true);
        });

      muteMember.send ... 

目前它将运行errors 函数,然后继续执行.catch 之后的代码,例如muteMember.send。在运行 .catch 内部的内容之后,让它“停止” 的最佳方式是什么?提前谢谢你

【问题讨论】:

    标签: javascript node.js error-handling discord.js try-catch


    【解决方案1】:

    如果发生错误,你可以让它返回一个falsy,然后检查它是否是falsy,如果是则返回。

    let isModified = await muteMember
            .timeout(time, reason)
            .catch((error) => {
                errors(client, message, args, "mute", error, true)
                return false;
            })
    if (!isModified) return;
    

    【讨论】:

      【解决方案2】:

      您可以将 async-await 与 try-catch 一起使用:

      async function myFunction()
      {
        try
        {
          await muteMember.timeout(time, reason)
          // throws an error if it fails -- jumps to the catch block
      
          muteMember.send...
        }
        catch(error)
        {
          errorcheck = true
          errors(client, message, args, "mute", error, true);
          // and whatever other error handling you would like
        }
      }

      【讨论】:

        【解决方案3】:

        return 语句仅从#catch 回调中返回。使用#then 回调处理承诺,这是您希望代码在成功时运行的位置。

        muteMember
            .timeout(time, reason)
            .catch((error) => {
                //error
                errorcheck = true
                return errors(client, message, args, "mute", error, true);
            })
            .then(() => {
                //success
            })
        

        【讨论】:

          【解决方案4】:

          muteMember.timeout() 返回一个Promise,所以你想要在promise 解决后运行的任何代码都应该包含在一个then() 块中:

          muteMember
                  .timeout(time, reason)
                  .then((member) => {
                    // code that depends on successfully timing out a member
                    muteMember.send....
                  })
                  .catch((error) => {
                    // only runs if there's an error
                    errorcheck = true
                    return errors(client, message, args, "mute", error, true);
                  });
          

          您还可以使用更现代、更易读的async/await 语法:

          const myAsyncFunction = async () => {
            try {
              const member = await muteMember.timeout(time, reason);
              // code that depends on successfully timing out a member
              muteMember.send....
            } catch (error) {  
              // only runs if there's an error
              errorcheck = true
              return errors(client, message, args, "mute", error, true);
            }
          }
              
          

          【讨论】:

            猜你喜欢
            • 2015-02-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-08-28
            • 1970-01-01
            • 2016-12-08
            • 1970-01-01
            相关资源
            最近更新 更多