2

I have a simple script which just take all values from Redis list and print them to console.

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?

kharandziuk
  • 12,020
  • 17
  • 63
  • 121

1 Answers1

3

A simple solution is to call process.exit inside your onValue handler.

).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.

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']
    );
  })
})
OlliM
  • 7,023
  • 1
  • 36
  • 47
  • 2
    Hi, your solution works. But I find the better one. I should close the redis connection with `client.quit();` – kharandziuk May 27 '15 at 07:44