async/await:connection.close()
callback:connection.close([function(err) {…}])
info:
- 为什么要使用setTimeout?延迟关闭通道,保证消息队列完成推送
//callback
(async () => {
amqp.connect(url, function (error, connection) {
if (error) throw error;
setTimeout(function () {
connection.close();
process.exit(0);
}, 500);
})
})()
// async/await
(async () => {
try {
const connection = await amqp.connect('url')
setTimeout(async () => {
await connection.close();
process.exit(0);
}, 500)
} catch (e) {
console.log(e)
}
})()