JavaScript中与Ruby的Array#compact相当的是什么?
长篇版本...我按照blog.nemikor.com上的示例进行操作。他的最后一个示例关闭旧请求,但是pendings
继续填充过时的请求。这对我来说看起来像是内存泄漏。
我的解决方案是使用下面的filter
迭代pendings
,但这似乎可能存在pendings.push
和pendings = pendings.filter
之间的竞争条件。我是多虑了吗?如果存在竞争条件,我该如何解决?
var pendings = [];
// there is a route
app.get('/some/path', function (request, response) {
pendings.push({
response: response,
requestedAt: new Date().getTime()
});
});
setInterval(function () {
var expiration = new Date().getTime() - (1000 * 30);
pendings = pendings.filter(function (pending, index) {
if (pending.requestedAt > expiration) {
return true;
} else {
pending.response.writeHead(408, { 'Content-Type': 'text/plain' });
pending.response.end('');
}
});
}, 1000);