每个吞咽异常的Java脚本

问题描述 投票:0回答:1

我正在尝试使用JavaScript中的forEach回调和async循环来遍历数组。问题是当我抛出异常时,它会给出

(node:2500) 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: 6)

尽管forEach之后有一个catch块。这是我的代码

array
.forEach(async (e) => {
    try {
        let bom = await BOMChild.findOne({
            where: { id: e.id },
        });

        if (bom.BOMProductId || bom.BOMProductId === 0) {
            throw {
                name: " error",
                description: `Description `,
            };
        }
    } catch (e) {
        throw e;
    }
})

我做错了什么,解决这个问题的可能方法是什么。TIA

javascript asynchronous promise async-await es6-promise
1个回答
1
投票

您在catch块中抛出了异常:

} catch (e) {
    throw e;
}

如果您以不同的方式处理,错误将消失:

} catch (e) {
    // do something else with e
}
© www.soinside.com 2019 - 2024. All rights reserved.