[英]Never ending stream with Baconjs
I have a simple script which just take all values from Redis list and print them to console. 我有一个简单的脚本,它仅从Redis列表中获取所有值并将其打印到控制台。
var redis = require("redis"),
client = redis.createClient();
Bacon = require('baconjs');
Bacon.repeat(function(){
return Bacon.fromNodeCallback(
client, 'lpop', ['errors']
);
})
.takeWhile(function(val) {return val !== null;})
.fold(
[],
function(acc, next) {
acc.push(next); return acc;
}
).onValue(console.log);
The program prints the correct list but never finishes. 该程序将打印正确的列表,但永远不会完成。 How can I fix the issue?
我该如何解决该问题? And why does it happen?
为什么会发生呢?
A simple solution is to call process.exit
inside your onValue handler. 一个简单的解决方案是在onValue处理程序中调用
process.exit
。
).onValue(function(value) {
console.log(value)
process.exit(0)
})
EDIT: You could write a custom redit source, which closes the connection when it is no longer needed. 编辑:您可以编写一个自定义的redit源,当不再需要该连接时,它将关闭连接。
Bacon.fromBinder(function(sink) {
var client = redis.createClient()
sink(new Bacon.Next(client))
return function unsubscribe() {
client.quit()
}
}).flatMap(function(client) {
return Bacon.repeat(function(){
return Bacon.fromNodeCallback(
client, 'lpop', ['errors']
);
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.