【发布时间】:2017-06-12 01:45:42
【问题描述】:
我是 NodeJS 的新手,我们使用的是 amqp 0.2.6。我有看起来像的伪代码
async.waterfall([
function(callback) {
// do logic and pass parameters to next method by calling callback
},
function(params, callback) {
// do AMQP logic and pass parameters to next method
try {
bus.publish(routingKey, message, options, function (err) {
console.log("got here");
if (err) {
console.log(err);
}
});
//callback(null, null);
} catch (err) {
console.log("ERROR: " + err);
//callback(err, null);
}
}
], function() {
// do logic with all results
});
所以我有几个问题。首先,当我调用发布时,我的回调有问题吗?我的消息被正确发送给兔子,但我从来没有收到回调got here 消息。
如果我取消注释以回调开头的两行,那么我在 async.waterfall 中的第二个函数会立即获取它的回调,然后调用它的回调将结果传递给 async.waterfall 中的最终函数。我明白这是因为回调在发布后立即被调用。我想要做的是理想情况下在我知道我发送给兔子的消息是否成功且没有错误或有错误之后调用回调。我不知道该怎么做,因为我无法访问 amqp 发布消息的回调。如果有错误,我确实在我的 try/catch 块中看到,我看到我的错误已记录,但这是在我的 async.waterfall 函数已经完成之后。我想将发布消息的结果(成功或失败)传递给 async.waterfall 中的第二个函数。我可以实现的任何非阻塞方式都可以。所以最终的目标是
async.waterfall([
function (one) {
// do logic and pass results to 'two'
},
function (two) {
// do AMQP logic, send a message, get the results of that action and pass to last function
}
], function() {
// i got my results from 'one' and 'two', do some more logic
});
【问题讨论】:
标签: javascript node.js asynchronous callback