Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

end(data) doesn't seem to fire the 'data' event #1

Open
coryvirok opened this issue Nov 28, 2012 · 1 comment
Open

end(data) doesn't seem to fire the 'data' event #1

coryvirok opened this issue Nov 28, 2012 · 1 comment

Comments

@coryvirok
Copy link

I just started looking into your event-stream library and it looks pretty awesome... I ran into this though.

var eventstream = require('event-stream');

var theData;
var endEmitted = false;
var ws = eventstream.map(function(data, callback) {
  return callback(null, data);
});

ws.on('data', function(data) {
  theData = data;
});
ws.on('end', function(err) {
  endEmitted = true;
});

function check() {
  if (endEmitted) {
    console.log('end emitted, theData: %s', theData);
  } else {
    setTimeout(check, 1000);
  }
};

setTimeout(check, 1000);

ws.end('OH HAI');

outputs:

end emitted, theData: undefined

If I change it to use

ws.write('OH HAI');
ws.end();

we get

end emitted, theData: OH HAI
@dominictarr
Copy link
Owner

you don't need a timeout to check this, 'data' should always emit strictly before 'end'.
that said, end(data) isn't strictly a part of Streams, it's a convenience api.

I'll merge a pull request if you make one, else use ws.write(data)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants