What is JavaScript equivalent to Ruby's Array#compact?
Long version.... I followed the examples at blog.nemikor.com. His last example closes out old requests, but then pendings
continues to be filled with obsolete requests. This looks like a memory leak to me.
My solution is to iterate over pendings
with filter
as below, but this seems like there may be a race condition between pendings.push
and pendings = pendings.filter
. Am I being paranoid? If a race condition exists, how should I fix it?
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);