[英]async.waterfall bind context
I am currently working on a web application with node.js and I can't figure a context problem with the library async. 我目前正在使用node.js处理Web应用程序,我无法解决库异步的上下文问题。
Here is a example of code of my application : 这是我的应用程序的代码示例:
notification.prototype.save = function (callback) {
async.parallel([
// Save the notification and associate it with the doodle
function _saveNotification (done) {
var query = 'INSERT INTO notification (notification_id, user_id, doodle_id, schedule_id) values (?, ?, ?, ?)';
notification.db.execute(query, [ this.notification_id, this.user_id, this.doodle_id, this.schedule_id ], { prepare : true }, function (err) {
return done(err);
});
console.log("SAVE NOTIFICATION");
console.log("doodle_id", this.doodle_id);
}.bind(this),
// Save notification for the users with the good profile configuration
function _saveNotificationForUsers (done) {
this.saveNotificationForUsers(done);
}.bind(this)
], function (err) {
return callback(err);
});
};
So in this code, I have to use the bind method to bind the context of my object ( this ) because otherwise async change it. 所以在这段代码中,我必须使用bind方法绑定我的对象的上下文(this),否则异步更改它。 I got it. 我知道了。 But what I don't understand is why the code of this.saveNotificationForUsers does not work the same way : 但我不明白为什么this.saveNotificationForUsers的代码不能以相同的方式工作:
notification.prototype.saveNotificationForUsers = function (callback) {
console.log("SAVE NOTIFICATION FOR USERS");
console.log("doodle id : ", this.doodle_id);
async.waterfall([
// Get the users of the doodle
function _getDoodleUsers (finish) {
var query = 'SELECT user_id FROM users_by_doodle WHERE doodle_id = ?';
notification.db.execute(query, [ this.doodle_id ], { prepare : true }, function (err, result){
if (err || result.rows.length === 0) {
return finish(err);
}
console.log("GET DOODLE USERS");
console.log("doodle id : ", this.doodle_id);
return finish(err, result.rows);
});
}.bind(this)
], function (err) {
return callback(err);
});
};
When I call the previous code, the first console.log is able to show me the "this.doodle_id" variable, which means the function knows the "this" context. 当我调用前面的代码时,第一个console.log能够显示“this.doodle_id”变量,这意味着该函数知道“this”上下文。 But the functions inside the waterfall call does not, even if I bind 'this' to them. 但是瀑布调用中的函数没有,即使我将'this'绑定到它们。
I figured a way to make it works by creating a 'me' variable which is equal to 'this' juste before I call waterfall, and by binding the functions with the 'me' variable' and not this, but I would like to understand why I am forced to do this when I use async.waterfall and not when I use async.parallel. 我想通过在调用瀑布之前创建一个等于'this'juste的'me'变量,并通过将函数与'me'变量绑定'来实现它的工作方式,而不是这个,但我想了解为什么我在使用async.waterfall时被迫这样做,而不是在我使用async.parallel时。
I hope I was clear with the description of my problem, if someone can help me understand it will be a great pleasure ! 我希望我能清楚地描述我的问题,如果有人能帮助我理解这将是一件非常愉快的事情!
The problem you're seeing has got nothing to do with parallel or waterfall but rather how in the waterfall
case, you're referencing this
in the callback to notification.db.execute
, whereas in the parallel
case, there is only a call to done
in there. 你看到这个问题已经什么都没有做平行或瀑布而是如何在waterfall
的情况下,如果引用this
回调至notification.db.execute
,而在parallel
的情况下,只有一个呼叫在那里done
。 You could use bind
again to bind that callback as well: 您可以再次使用bind
来绑定该回调:
async.waterfall([
function _getDoodleUsers (finish) {
//…
notification.db.execute(query, [ this.doodle_id ], { prepare : true }, function (err, result){
//…
}.bind(this)); // <- this line
}.bind(this)
], function (err) {
//…
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.