small medium large xlarge

Back to: All Forums  CoffeeScript
17 Sep 2011, 04:02
Edward Jenkins (4 posts)

I’ve got what is probably a very stupid question, but I’ve been re-reading the relevant part of the book over and over, and still can’t seem to get it: On page 31 in the 5x5 input parser section, the

stdin.on 'data', (input) -> inputCallback input

line keeps tripping me up. It looks like we are passing ‘data’ and an anonymous function ‘(input) -> inputCallback input’ as arguments to stdin.on. What is ‘data’, exactly? To me, it just looks like a string. But what’s it for? Where does it come from? I feel like I’m missing something completely obvious.

17 Sep 2011, 23:47
Trevor Burnham (53 posts)

Perfectly reasonable question. The @on@ method comes from Node’s @EventEmitter@ (, and takes as its first argument the name of an event. Internally, what happens is that when @stdin@ receives input, something calls @stdin.emit ‘data’, input@, and then every callback attached with @stdin.on ‘data’@ gets called with that @input@.

So, you’re right: It’s weird that the string @’data’@ shows up out of nowhere. You’d have to look at the documentation for Node’s @process.stdin@ ( to know that it emits events with that name.

By the way, this is basically identical to how jQuery’s @bind@ and @trigger@ work. (You’ve probably seen code like @$(‘#myButton’).click callback@, which is just a shorthand for @$(‘#myButton’).bind ‘click’, callback@.)

Hope that helps!

19 Sep 2011, 01:18
Edward Jenkins (4 posts)

Thanks for that great, prompt explanation. So relieved that I was on the right track!

You must be logged in to comment