javascript - 未处理的拒绝nodejs

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

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

我读过这样的文章 In Node.js 7 what is the proper way to suppress UnhandledPromiseRejectionWarning?但由于事件监听器泄漏,我没有使用 on,而是使用 once,但不知何故,有时我仍然会看到警告

我确实想摆脱这个警告或解决它,因为它说将来会被弃用,但不确定什么时候。

首先,当我第一次运行时,我会先得到这个

您触发了未处理的拒绝,您可能忘记捕获 Promise 拒绝:myrejectionmessage

然后,我会收到此错误

UnhandledPromiseRejectionWarning:myrejectionmessage UnhandledPromiseRejectionWarning:未处理的 promise 拒绝。此错误的根源是在没有 catch block 的情况下抛出异步函数内部,或者拒绝未使用 .catch() 处理的 Promise。 (拒绝 ID:5)

这是我的原始代码,没有我对找到的帖子进行的尝试,我正在尝试在 aws s3 存储桶 中获取一些文件,但存储桶中的文件可能不存在

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

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');
    }
};

最佳答案

//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 中读取 File 后的回调..它不会捕获来自等待的拒绝错误

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

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');
}

};

enter image description here

关于javascript - 未处理的拒绝nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53941497/

相关文章:

php - display_errors ON无效(Apache 2.4.7和PHP 5.5.8-2)

javascript - 带有 Google 前置广告的 HTML5 视频播放器

javascript - jQuery:切换一个类并替换为另一个类 - 反之亦然

node.js - 如何在快速错误处理中使用 return

node.js - Electron 原生插件在Windows上失败

php - 为什么PaaS提供商建议在部署时安装依赖项? (对于 PHP 或 NodeJS 以及可能其他)

python-3.x - 有人可以帮我吗。我在用Python编写此代码时遇到问题

javascript - div 不与其他人一起移动

javascript - jQuery 祖先使用 jQuery 对象

node.js - 避免总计 16MB 的限制