I'm working on a chat project, NodeJS has to wait 25 secs for new data from MongoDB, any alternatives?
while(true) {
db.messages.find({
...
}).then(result) => {
if (result.length > 0) {
return result;
}
})
}
UPDATE:
let secs = 0;
const iv = setInterval(() => {
secs++;
db.messages.find({
...
}).then(result) => {
if (result.length > 0) {
clearInterval(iv);
res.json(result);
return;
}
});
if (secs === 25)
clearInterval(iv);
}, 1000);