【问题标题】:unhandledRejection nodejsunhandledRejection nodejs
【发布时间】:2018-12-27 18:47:55
【问题描述】:

我知道有很多关于此错误的帖子,其中大多数都有相同的答案,但不知何故我仍然收到警告。

我读过类似In Node.js 7 what is the proper way to suppress UnhandledPromiseRejectionWarning? 的内容,但由于事件侦听器泄漏,我使用once 而不是on,但不知何故我有时仍会看到警告

我确实想摆脱警告或解决它,因为它的说法将来会被弃用,但不确定何时。

起初我第一次跑步时,我会先得到这个

You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection: myrejectionmessage

然后,我会得到这个错误

UnhandledPromiseRejectionWarning: myrejectionmessage UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)

这是我的原始代码,没有我尝试过的帖子,我试图在aws s3 bucket 中获取一些文件,但桶中的文件可能不存在

这个功能是比较是否有文件然后比较修改时间,如果文件不存在reject

exports.compareObjectMT = (s3, Key, getFileStat) => {
    const s3GetParams = {
        Bucket: process.env.S3_BUCKET,
        Key,
    };

    return new Promise((res, rej) => {
        s3.getObject(s3GetParams, (err, data) => {
            if (err) rej('myrejecterror');
            if (data) {
                res(String(getFileStat.mtimeMs) === data.Metadata.mtimems);
            }
            res(false);
        });
    });
};

提前感谢您的任何建议

这就是我使用函数的方式

exports.s3Put = async (path) => {
    try {
        fs.readFile(path, async (err, fileBinary) => {
            if (err) throw err;
            // console.log(data, 'data');
            const s3 = new AWS.S3();
            const Key = path.replace(process.env.WATCH_PATH, '');
            const getStat = await getFileStat(path);
            console.log(getStat, 'getstateeeeeeeeeeeeeeee');
            const compareObj = await compareObjectMT(s3, Key, getStat);
            console.log(compareObj, 'compareObj');
        });
    } catch (e) {
        console.log(e, 'errorrrrrrrrrrrrr');
    }
};

【问题讨论】:

  • 您需要compareObjectMT 的消费者来捕获错误。
  • @CertainPerformance 我确实尝试将函数的内部包裹在 const s3Get..... 直到函数结束时使用 try catch though
  • 不是try/catch,而是.catchPromise方法

标签: javascript node.js error-handling es6-promise


【解决方案1】:
//calling compareObjectMT ,Your return value is a Promise Object either resolve/reject

//s3, Key, getFileStat aruments value you are passing

compareObjectMT(s3, Key, getFileStat).then((value)=>{do something}) 
                                     .catch((err)=>console.error(err))

你正在做的类似于这个..你在读取文件后的回调 try catch..它不会从等待中捕获拒绝错误

你可以将所有 await 放在单个 try catch 块中

exports.s3Put = async (path) => {
try {
    fs.readFile(path, async (err, fileBinary) => {
        if (err) throw err;
        // console.log(data, 'data');
         try {
        const s3 = new AWS.S3();
        const Key = path.replace(process.env.WATCH_PATH, '');
        const getStat = await getFileStat(path);
        console.log(getStat, 'getstateeeeeeeeeeeeeeee');
        const compareObj = await compareObjectMT(s3, Key, getStat);
        console.log(compareObj, 'compareObj');
      }catch (e) {
    console.log(e, 'errorrrrrrrrrrrrr');
}
    });
} catch (e) {
    console.log(e, 'errorrrrrrrrrrrrr');
}

};

【讨论】:

  • 我想我知道为什么我现在一直收到警告,因为我想使用asyncawait 并且我一直在做const co = await compareObjectMT(s3, Key, getFileStat) 如果我想这样做,有可能吗?
  • await 必须在 async 函数内部使用,需要放入 try{const co = await compareObjectMT(s3, Key, getFileStat)}catch(e){}
  • 我编辑了我的代码来展示我如何实际使用compareObjectMT
  • const compareObj = await compareObjectMT(s3, Key, getStat);为此,您没有任何错误处理程序,它不会在 catch 块之外被捕获,因为您必须抛出错误,对于您添加的外部 catch 块
  • 谢谢!我相信这就像一种魅力。我一直认为只有一个 try / catch 即使嵌套也能捕获所有内容,但这次是因为它在另一个 fs.readFile() 中,这就是为什么如果我理解正确的话它没有捕获它的原因。我只需要弄清楚其余的,因为不知何故不再按预期工作:|
猜你喜欢
  • 2020-06-14
  • 2020-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-11
  • 2018-07-07
  • 2018-12-08
  • 2019-05-23
相关资源
最近更新 更多