【发布时间】:2013-02-12 11:06:48
【问题描述】:
我正在尝试缩小我的 node.js 服务器出现内存问题的潜在原因。我一直有点不舒服的代码的一部分是我对 Q Promise 的使用。
这是我的基本结构:
var Q = require('q');
MyClass.prototype.doSomething = function(somedata, callback) {
var res = [];// will contain the results of each function call
Q.ninvoke(this, 'doSomethingElse', 'hello!')
.then((function(result){
res.push(result);
return Q.ninvoke(this.someobject, 'someFunction', somedata);
}).bind(this))
.then((function(result){
res.push(result);
callback(null, res);// Returns both result objects, in an array
}).bind(this))
.fail(callback)
.done();
}
这看起来合乎逻辑吗?
如果 doSomethingElse 函数也使用 Promise 会怎样?这里的所有范围都正确吗?
【问题讨论】: