【发布时间】:2020-06-04 05:02:24
【问题描述】:
我有以下代码,它可以工作,但仍然会引发警告。 我在 Node v12 中运行。
(node:15985) 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: 3)
这是循环的源代码,谢谢:
const links = await knex('links').where('active', true);
const links = await knex('links').where('active', true).catch((e) => console.log(e)) // does not work neither
for (let index = 0; index < links.length; index++) {
const element = links[index];
console.log('Running with', index, element.uri);
(async () => {
try {
const { statusCode } = await got({
url: `${element.protocol}://${element.uri}:${element.port}`,
method: 'GET',
timeout: 5000
})
const check = {
statusCode: statusCode,
}
await knex('checks').insert(check);
} catch (error) {
const check = {
status: 'error',
}
await knex('checks').insert(check);
}
})().catch(() => {});
}
【问题讨论】:
-
我猜这是第一个
await knex,因为那里没有catch。 -
我替换为:await knex('monitors').where('active', true).catch((e) => console.log(e)),但还是一样的错误
标签: node.js promise async-await