On Apr 28, 2012, at 3:22 AM, Mark Volkmann wrote: > This is a question about recommended usage in Node. > As an example, let's say I want to write a function that returns the > first n prime numbers. > I could write a function that takes n and a callback. > It could invoke the callback once for each prime number to be returned > and then invoke it with zero to signal the end. > > I could also write a function that takes n and returns a readable stream. > The caller could listen for data events (one per prime number to be returned) > and an end event. > > I'm pretty sure the stream approach is preferred, > but would you say that the callback approach is wrong or at least discouraged?
I would say that in node calling a callback more than once is not only discouraged, but forbidden, as if it were part of an implied, non written contract. It seems that every function of node's API that would need to call a cb more than once, has been implemented as an event emitter, apparently on purpose. I for one don't know the reason why. In my mind things like fs.readFile(..., cb) could better return the file in small chunks by calling cb(chunk) repeatedly, instead of buffering it all. -- Jorge. -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
