我编写了这段代码,使我可以在Node.js中创建可读流,并尽可能频繁地将数据传出双端队列:
const limiter = new RateLimiter(count, interval)
const deque = new Deque()
const getStream = () => {
const inStream = new Readable({
objectMode: true,
read() {}
})
setInterval(() => {
if (deque.length >= count) {
const result = limiter.tryRemoveTokens(count)
if (result) {
for (let i = 0; i < count; i++) {
inStream.push(deque.shift())
}
}
}
}, 1)
return inStream
}
每隔1毫秒执行一次setInterval是一种好习惯吗?