I'm not sure if there are extra parameters I need to worry about with async.parallel or not. I tried a couple contrived examples with setTimeout to understand it, and those work without issue. But when I try to use it in my actual code, it doesn't work. Here is the code I want to work (the important parts)
async.parallel([
function(callback) {
dbConn.query()
.insert(tableName, ["HOST", "SERVER_NAME", "DATE_CREATED"], [hostname, serverName, currentTime])
.execute(function (err, result) {
if (err) {
console.log(err);
} else {
console.log("first part complete");
dbConn.query()
.select("ID").from(tableName).where("ID=?", [result.id])
.execute(function (err, rows, cols) {
if (err) {
console.log(err);
} else {
console.log("second part complete");
callback();
}
});
}
});
},
function (callback) {
rabbitUtils.sendMessage(123, function (err, result) {
if (err) {
console.log(err);
} else {
console.log("messaging done");
callback();
}
})
}
], function() {
console.log("i'm done");
});
The first async parallel function basically inserts a record and reads a record from a database table. The second function in async parallel sends off a message to rabbitmq. Whenever either of the parallel tasks (db or rabbit) finishes, "i'm done" gets called. What I have also tried is just doing an insert into the database, and not the reading after the insert and for some reason, it does work as expected where "i'm done" does not get printed out until a db insert and rabbit message happens. But once I add inserting and reading from a db as one function call, and the rabbit as another function call, the final callback gets called right away. Is there a reason why this happens? I could rewrite it using async.waterfall I suppose and see if that gets the behavior I want, but I just don't see why async.parallel in this scenario does not work. Thanks in advance.
via Crystal
No comments:
Post a Comment