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.
Putting a callback where one is not yet needed is fine, for mock-up where something more complicated is inserted later. To simulate right mechanics, it might be a good idea to user process.nextTick(). Its not good style, but some calling code gets confused if the callback is executed immediately even before the wrapper function returns. On Sat, Mar 10, 2012 at 11:32 PM, Angelo Chen <[email protected]> wrote: > 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 <[email protected]> wrote: >> 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] ? null : "NOT_FOUND", users[id] || null); >> >> } >> >> I wonder, why you need a callback after all? Its not an IO operation >> with a delay. If not needed due to overall structure or so, it gets >> even simpler: >> >> exports.get_user = function(id) { >> return users[id] || null; >> >> >> >> >> >> >> >> } > > -- > 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 -- 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
