避免跳过Node.js中函数的执行

时间:2017-09-25 07:18:13

标签: node.js

我在Node.js应用程序中有一个以下函数。应用程序跳过此功能的执行。我已经尝试使用wait.for.function()等待整个函数完成它的执行。但是wait.for.function()在执行函数中的所有行之后无限期地等待。

function check(msg)
{
  amqp.connect('amqp://serv1:password1@localhost', function(err, conn) {
    conn.createChannel(function(err, ch) {
      var ex = 'RouteActions';
      var msg='hello'
      ch.assertExchange(ex, 'direct', {durable: false});
      ch.publish(ex, 'vm1', new Buffer(msg));
      console.log(" [x] Sent %s", msg);
      return;
    });
  });
}

1 个答案:

答案 0 :(得分:0)

我没有足够的声誉点来发表评论,所以你能告诉我你调用这个函数的代码片段吗?还提供了一个回调函数来同步执行它。

尝试这样的事情:

function check(msg, checkCallback) {
    amqp.connect('amqp://serv1:password1@localhost', function(err, conn) {
        conn.createChannel(function(errCreate, ch) {
            if (errCreate || err) {
                return checkCallback("Oops something went wrong while establishing connection...");
            } else {
                var ex = 'RouteActions';
                var msg = 'hello';
                ch.assertExchange(ex, 'direct', {
                    durable: false
                });
                ch.publish(ex, 'vm1', new Buffer(msg));
                console.log(" [x] Sent %s", msg);
                return checkCallback(null, "Connection established successfully...");
            }
        });
    });
}

//Function call
check("hello", function(err, resp) {
    if (err) {
        //Your code to handle connection failure
    } else {
        // resp gives you string "Connection established successfully...",
        //Do whatever you wanted to do after successful connection or a successful call to check method...
    }
});

这不是一个确切的代码,应该更优雅地处理错误,但我希望它应该足以实现你想要做的事情。

编辑:您可以通过要求

来使用此ampqlib的回调API
var amqp = require('amqplib/callback_api');

//instead of 

//var amqp = require('amqplib');

function check(msg, checkCallback) {
  amqp.connect('amqp://serv1:password1@localhost', function(err, conn) {
    conn.createChannel(function(errCreate, ch) {
      if (errCreate || err) {
        return checkCallback("Oops something went wrong while establishing connection...");
      } else {
        var ex = 'RouteActions';
        var msg = 'hello';
        ch.assertExchange(ex, 'direct', {
          durable: false
        }, function(exchangeErr, ok) {
          if (exchangeErr) {
            console.log("Error occurred while exchange..." + JSON.stringify(exchangeErr));
            return checkCallback(null, "Connection established successfully...");
          } else {
            ch.publish(ex, 'vm1', new Buffer(msg));
            console.log(" [x] Sent %s", msg);
            return checkCallback(null, "Connection established successfully...");
          }
        });
      }
    });
  });
}

根据ampqlib的文档,他们提供了基于promise的API以及基于回调的功能,所以我提供了一个使用回调的解决方案,请告诉我这是否也可以将日志放在我的代码片段中,这样如果它有一些你知道的错误。

编辑2

这是我尝试过的非常好,而且效果非常好......

function check(msg, checkCallback) {
  amqp.connect('amqp://localhost', function(err, conn) {
    console.log("This is err: " + JSON.stringify(err) + " conn:: ");
    conn.createChannel(function(errCreate, ch) {
      console.log("This is errCreate:: " + JSON.stringify(errCreate) + " ch:: ", ch);
      if (errCreate || err) {
        return checkCallback("Oops something went wrong while establishing connection...");
      } else {
        var ex = 'RouteActions';
        var msg = 'hello';
        ch.assertExchange(ex, 'direct', {
          durable: false
        }, function(exchangeErr, ok) {
          console.log("This is exchangeErr:: " + JSON.stringify(exchangeErr) + " ok:: ", ok);
          if (exchangeErr) {
            console.log("Error occurred while exchange..." + JSON.stringify(exchangeErr));
            return checkCallback(null, "Connection established successfully...");
          } else {
            ch.publish(ex, 'vm1', new Buffer(msg));
            console.log(" [x] Sent %s", msg);
            return checkCallback(null, "Connection established successfully...");
          }
        });
      }
    });
  });
}


check('Hii', function(error, resp) {
if(error){
console.log(error);
}else{
//Rest of your code in my case...
console.log("All fine good to go...");
}
});