1
說我有下面的代碼:等待貓鼬查詢結束
function MyClass(id, x, y, z) {
var self = this;
MyModel.findByID(id, function(err, model) {
if(err) { // Do something }
self.model = model
});
this.x = x;
this.y = y;
this.z = z;
}
MyClass.prototype.myMethod = function() {
console.log(this.model.id);
}
var obj = new MyClass(123, 7, 8, 9);
obj.myMethod();
我怎樣才能確保this.model
在myMethod
設置,因爲貓鼬查詢是asyncronous。還是我用'非'NodeJS方式編程?
感謝
很明顯,謝謝 –