Neither!

I think that question should be callbacks vs. events vs. streams:
   
   - Callback: called only once, when the function is done, to return 
   result (optional) or error.
   - Event: called repeatedly by the function to notify its listeners. Can 
   be used to send intermediate results as they come.
   - Stream: higher level concept based on events, with standardized event 
   types (data, end, error, drain) and standardized API (pause, resume, write, 
   ..).

So, assuming prime computation is asynchronous:

   - If the function computes the N first primes and returns them all at 
   once, it should use a callback.
   - If the function returns the primes one by one, it should use events. 
   It may also use a stream but that seems a bit over-engineered.


On Saturday, April 28, 2012 3:22:10 AM UTC+2, 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? 
>
> -- 
> 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 [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

Reply via email to