No way to detect list end #13

Open
nponeccop opened this Issue Jan 24, 2012 · 3 comments

3 participants

@nponeccop

Use case:

I need to read data from file A and then use the data to process file B:

var foo = {}
A.lines.forEach(function (x) {  foo[getKey(x) = getValue(x) })
B.lines.forEach(function (x) {  if foo[getKey(x) then console.log('yo!') else console.log('yay!')  })

The code doesn't work as intended because the two loops execute in parallel. So I need something like this:

A.lines.forEach(function (x) {  foo[getKey(x) = getValue(x) }).then(function ()
{
     B.lines.forEach(function (x) {  if foo[getKey(x) then console.log('yo!') else console.log('yay!')  })    
})
@juliankrispel

Got the same problem. Would be nice if somebody fixes this, here's A workaround for it so far: http://stackoverflow.com/questions/6942101/knowing-eof-when-using-node-js-and-lazy

@nponeccop

I use a better workaround (discovered by reverse engineering Lazy):

myStream
    .filter(foo)
    .forEach(bar)
    .on('pipe', baz)

'pipe' event is emitted by the streams on end of stream, so you can have as much subscribers to them as you want. However, I found it better to create a promise, resolve a promise in the handler and use the promise elsewhere (see https://github.com/kriskowal/q ).

@joeferner

This is what worked for me...

lazy(myStream)
  .on('end', function() { console.log('end'); }
  .lines
  .forEach(function(line) {
     console.log(line);
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment