当我使用async await函数时,我得到了弃用警告: unhandled promise rejections are deprecated
. 此错误源于通过抛出异步函数而没有catch块,或者拒绝未使用.catch()处理的promise .
export const publish = async (exchange, type, message) => {
try {
const connection = await amqplib.connect(connectionString)
const channel = await connection.createChannel()
channel.assertExchange(exchange, config.messageQueue.exchange.type, {durable: true})
logger.silly(`publishing message to ${exchange}`)
channel.publish(exchange, '', type.encode(message).finish())
}
catch (e) {
logger.warn(`error while publishing message, ${e}`)
throw e
}
}
const startConsuming = async () => {
try {
const {Get, Sort, Cleanup, ...elasticMessages} = expectedMessages
await init(Object.keys(elasticMessages))
amqpService(expectedMessages, onMessage)
publish(`${config.messageQueue.exchange.prefix}Cleanup`, contracts.televic.historyLogging.Call, '')
} catch (e) {
logger.error('error while establishing connection to message bus', e)
}
}
1 回答
您之前没有
await
或之后没有.catch
:但是
publish
是一个async
函数,所以这个函数不会抛出错误,但是它返回的Promise将被拒绝并出错 . 因此,如果在catch
块中调用throw e
,那么publish
将被拒绝并显示该错误,并且不会处理此拒绝 .所以代码必须看起来那样: