Re: [nodejs] Re: which one is correct ?

2012-03-12 Thread Axel Kittenberger
Simply said, you need a callback, when you have to do one. This means, you use another IO-related API that has a callback. Or a little more elaborate, you need a callback when your code waits for something, most times disc or network data, and thus the current flow logic stalls until it arrives.

[nodejs] Re: which one is correct ?

2012-03-10 Thread Angelo Chen
Hi All, surprised to see many discussions about this, I'd say i learnt a lot: 1) return from loop 2) Array.some 3) return error instead of string 4) string is faster sometimes all are very valuable, after evaluating my use case, I choose 2 and 4, and reserve the rest for future use, very

Re: [nodejs] Re: which one is correct ?

2012-03-10 Thread Axel Kittenberger
If your user are not in a particular order, add to this: don't use an array if there is no order, but a hash (object). users = {}; e.g. with adding a user by: users[id] = {|somenewuserdata|}; In that case getting a new user is easy as: exports.get_user = function(id, func) { func(users[id]

[nodejs] Re: which one is correct ?

2012-03-10 Thread Angelo Chen
This does bring out another topic, when we need a call back? Any rule ? In this case the function wii be replaced with a database one later, so it might have some IO. On Mar 10, 11:49 pm, Axel Kittenberger axk...@gmail.com wrote: If your user are not in a particular order, add to this: don't

[nodejs] Re: which one is correct ?

2012-03-09 Thread Angelo Chen
oh, return is cool! On Mar 9, 7:02 pm, Axel Kittenberger axk...@gmail.com wrote: Many coders forget that you can use the return statement before the end of the function. At least most people who finished some coding course hardly ever do this. It usually simplifies many functions:

Re: [nodejs] Re: which one is correct ?

2012-03-09 Thread Martin Wawrusch
On Fri, Mar 9, 2012 at 10:36 AM, Roly Fentanes roly...@gmail.com wrote: btw http://www.devthought.com/2011/12/22/a-string-is-not-an-error/ +1 for that, and while we are at it: The whole thread is a great example why it is so important to use libraries like underscore's find:

Re: [nodejs] Re: which one is correct ?

2012-03-09 Thread Mark Volkmann
For what it's worth, I was trying to minimize the changes to the function and mainly demonstrate the use of the Array some method for iteration that can benefit from stopping early. --- R. Mark Volkmann Object Computing, Inc. On Mar 9, 2012, at 12:49 PM, Martin Wawrusch mar...@wawrusch.com

[nodejs] Re: which one is correct ?

2012-03-09 Thread Jimb Esser
Though Errors definitely have their place, such as when an error occurs, using an error for regular return codes during expected events (assuming this function expects the look up to fail some times) is a bit overkill, and horrible for performance. http://jsperf.com/a-string-is-not-an-error On