我有一个相对简单的例子来解释我所看到的奇怪问题:
this.applyActions = function (name, arg1, arg2, arg3, arg4, arg5, arg6) {
var promise = new RSVP.Promise(function (resolve, reject) {
// resolve all the functions
RSVP.hashSettled(this.actions[name])
//if it fails, just resolve to an empty object
.catch(function (reason) {
resolve({});
})
//resolve the functions into their promises, as designed
.then(function (funcs) {
for (var key in funcs) {
console.log(key);
funcs[key] = funcs[key].value(arg1, arg2, arg3, arg4, arg5, arg6);
}
return funcs;
})
//fulfill all the promises to get the results
.then(function (promises) {
return RSVP.hashSettled(promises);
}, function (reason) { console.log(reason); })
.then(function (results) {
resolve(results);
});;
}.bind(this));
return promise;
};
基本上,当rsvp做到这一点时,在定义和解析函数之间可能需要相当长的时间(300毫秒或更长时间)。换句话说,当我们到达解析函数的部分时,函数ApplyAction参数已经丢失了它们的范围并被垃圾收集。
我一直在试图想办法阻止GC(并保持我的闭包)或获取匿名函数的参数。
有没有人知道如何解决这个问题?