Streams are event emitters of course. Change your example to '.on('data', function (prime)' and you have a stream. In this case there is only one kind of event (and no 'end'!), so using a stream would let you pipe and other streamy stuff.
Steve On Fri, Apr 27, 2012 at 9:13 PM, Nathan Rajlich <nat...@tootallnate.net>wrote: > For times when a callback will be invoked multiple times, it's better to > use an EventEmitter: > > primeGenerator.on('prime', function (prime) { > // do stuff with num > }); > > This allows other interested parties to be able to get the data as well. > > > On Fri, Apr 27, 2012 at 7:09 PM, Mark Volkmann > <r.mark.volkm...@gmail.com>wrote: > >> Suppose though that it's a long-running operation that may or may not >> involve I/O. Both approaches would work. Is there a clear bias toward >> streams? >> >> --- >> R. Mark Volkmann >> Object Computing, Inc. >> >> On Apr 27, 2012, at 8:45 PM, Tim Caswell <t...@creationix.com> wrote: >> >> Since it's a sync operation, I'm assuming no threading tricks, then the >> question is sync callback vs buffering. Sync callback has more more >> overhead since it's many more function calls, but buffering has it's >> drawbacks if you're not going to need all the answers at once. >> >> // buffering >> var primes = getPrimes(n) >> >> // iterator >> getPrimes(n, function (prime) { >> }); >> >> On Fri, Apr 27, 2012 at 8:27 PM, Steve Molitor <stevemoli...@gmail.com>wrote: >> >>> Interesting question! I'm looking forward to the answer. >>> >>> Both streams and callbacks usually involve IO, but calculating prime >>> number would not. So it might be a special case. But still interesting. >>> >>> Steve >>> >>> >>> On Fri, Apr 27, 2012 at 8:22 PM, Mark Volkmann < >>> r.mark.volkm...@gmail.com> 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? >>>> >>>> -- >>>> R. Mark Volkmann >>>> Object Computing, Inc. >>>> >>>> -- >>>> 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 nodejs@googlegroups.com >>>> To unsubscribe from this group, send email to >>>> nodejs+unsubscr...@googlegroups.com >>>> For more options, visit this group at >>>> http://groups.google.com/group/nodejs?hl=en?hl=en >>>> >>> >>> -- >>> 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 nodejs@googlegroups.com >>> To unsubscribe from this group, send email to >>> nodejs+unsubscr...@googlegroups.com >>> For more options, visit this group at >>> http://groups.google.com/group/nodejs?hl=en?hl=en >>> >> >> -- >> 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 nodejs@googlegroups.com >> To unsubscribe from this group, send email to >> nodejs+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/nodejs?hl=en?hl=en >> >> -- >> 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 nodejs@googlegroups.com >> To unsubscribe from this group, send email to >> nodejs+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/nodejs?hl=en?hl=en >> > > -- > 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 nodejs@googlegroups.com > To unsubscribe from this group, send email to > nodejs+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > -- 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en