Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rabbitmq async await
var amqp = require('amqplib')
var open = require('amqplib').connect('amqp://localhost');
const connect = (url = 'amqp://localhost') => {
return new Promise((resolve, reject) => {
amqp.connect(url)
.then(conn => resolve(conn))
.catch(err => reject(err))
})
}
const createChannel = conn => {
return new Promise((resolve, reject) => {
conn.createChannel()
.then(channel => resolve(channel))
.catch(err => reject(err))
})
}
const channelAssertQueue = (channel, queueName) => {
return new Promise((resolve, reject) => {
channel.assertQueue(queueName)
.then(asserted => resolve(channel))
.catch(err => reject(err))
})
}
const sendToQueue = (channel, queueName, buffer) => {
channel.sendToQueue(queueName, buffer)
}
const connection = async (queueName = 'msg.*') => {
var conn = await connect()
var channel = await createChannel(conn)
var assertedChannelToQueue = await channelAssertQueue(channel, queueName)
return channel
}
module.exports = connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment