寻求有关使用承诺的nodejs专家的帮助.我有以下测试程序,其中我调用异步“q”函数,只是抛出异常.这个程序非常一致地泄漏内存;但如果取消注释.done()调用,泄漏就会消失.
当promise未终止时(即没有done()调用),为什么会发生泄漏?我试图遵循documentation,但无法理解done()方法的解释.在此先感谢您的帮助!
这是我的代码:
(function() { var MAX_ITER_COUNT,Q,iterCount,maxMem,noop,qDoit,test; Q = require("q"); iterCount = 0; MAX_ITER_COUNT = 10 * 1000; maxMem = 0; noop = function() {}; qDoit = function() { var currentMem; currentMem = Math.round(process.memoryUsage().heapUsed / 1024 / 1024); if (currentMem > maxMem) { maxMem = currentMem; } console.log("" + iterCount + " - memory is: " + currentMem + "/" + maxMem + " MB"); return Q(10).then(function() { throw new Error("X"); }); }; test = function() { if (iterCount++ > MAX_ITER_COUNT) { console.log("DONE"); return; } // ---- If I uncomment the done() call below the leak goes away ---- return qDoit()["finally"](function() { return setImmediate(test); }) //.done(noop,noop); }; Q.onerror = function() {}; test(); }).call(this);