0

I have to create a number of RabbitMQ channels in succession, so I am trying to parameterize their creation in a function whose return value would be the channel. The function is declared in the callback-of-a-callback to the connection function.

var otaChannel = null;
amqp.connect(amqp_url, (err0, conn) => {
  if(err0) {
    throw(err0);
  }


  function getConsumerChannel(channelname) {
    conn.createChannel((err1, ch) => {
      if(err1) {
        throw(err1);
      }

      ch.assertQueue(channelname, '', (err, q) => {
        console.log(' [*] Created ' + q.queue + '.');
        return ch;
      });
    });
  }

  otaChannel = getConsumerChannel(otaQName);
});

If I do "console.log(ch)" in the assertQueue function, I see the details of the channel. However, otaChannel in the final line of the code is always null. I've tried a bunch of variations of the code I posted, but cannot get the channel assigned to otaChannel. Where am I going wrong?

boing
  • 499
  • 2
  • 6
  • 22
  • You're trying to return a value from a callback for an asynchronous operation. – Pointy Apr 27 '19 at 15:47
  • As it stands, what were you expecting to retrieve? Is there any need to get the `otaChannel` at that point in your code? The duplicate will give you some good information and from there you can choose how to restructure your code (be it, your own callback, promise wrapper or async await) – Icepickle Apr 27 '19 at 15:50

0 Answers0