Hola tengo una pregunta muy importante que no entiendo.
Tengo 2 códigos con una variable libre llamada 'instancia'. en el primero, la variable permanece con su valor cada vez que necesito el archivo en la aplicación (valor de Promesa/Objeto), el segundo se menciona como indefinido cada vez que no lo inicialicé.
ahora sé que el primero es un singleton, pero no entiendo por qué en un caso puede contener los datos y en el otro caso no lo es y se vuelve indefinido cada vez.
let instance; class MessageBroker { async init() { this.connection = await amqp.connect(process.env.MQ_URL) this.channel = await this.connection.createChannel() return this } async pushToRabbitQueue(queueName, data) { if (!this.connection) { await this.init(); } await this.channel.assertQueue(queueName, {durable: true}); return this.channel.sendToQueue(queueName, Buffer.from(data)); } async listenToRabbitQueue(queueName, callback) { if (!this.connection) { await this.init(); } await this.channel.assertQueue(queueName, { durable: true }); this.channel.consume(queueName, callback, { noAck: true }); } } MessageBroker.getInstance = async function() { if (!instance) { const broker = new MessageBroker() instance = broker.init() } return instance }; module.exports = MessageBroker const connectToRMQChannel = async () => { const connection = await amqp.connect(process.env.MQ_URL); return await connection.createChannel(); } let instance; const pushToRabbitQueue = async (data, queueName = process.env.RABBITMQ_LEVEL2_LOGS_QUEUE_NAME) => { if (!instance) { instance= await connectToRMQChannel(); } return await instance.sendToQueue(queueName, Buffer.from(data)); } module.exports = { pushToRabbitQueue }gracias, me gusta mucho entenderlo.