That is not what the code is doing. The return that is in the function passed to Array.some is only used to determine when it can stop iterating through the array and what the return value of the some function will be. It does not return the from the get_user function.
On Fri, Mar 9, 2012 at 11:04 AM, Nathan Rajlich <nat...@tootallnate.net> wrote: > Except that you can't return in one case, then fire a callback in the > other case :\ > > On Fri, Mar 9, 2012 at 8:58 AM, Diogo Resende <drese...@thinkdigital.pt> > wrote: >> My mistake. Thinking about users.some() as not being Array.some() and >> possibly executed later on. Array.some() is perfect for this case. >> >> -- >> Diogo Resende >> >> >> On Friday, March 9, 2012 at 16:48 , Diogo Resende wrote: >> >>> That doesn't work.. >>> >>> -- >>> Diogo Resende >>> >>> >>> On Friday, March 9, 2012 at 16:40 , Mark Volkmann wrote: >>> >>> > Here's another approach that I prefer: >>> > >>> > exports.get_user = function (id, cb) { >>> > var user; >>> > users.some(function (u) { >>> > var found = u.id === id; >>> > if (found) user = u; >>> > return found; >>> > }); >>> > cb(user ? null : 'not found', user); >>> > } >>> > >>> > On Fri, Mar 9, 2012 at 5:02 AM, Axel Kittenberger <axk...@gmail.com >>> > (mailto: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: >>> > > >>> > > exports.get_user = function (id, func) { >>> > > for (var i = 0, z = users.length; i < z; i ++) { >>> > > var u = users[i]; >>> > > if (u.id === id) { >>> > > func(null, u); >>> > > return; >>> > > } >>> > > } >>> > > func('NOT_FOUND', null); >>> > > } >>> > > >>> > > Depending on optimizing speed vs memory use, or if this function is >>> > > heavly used compared to only seldomly (profiler!) l it might be a good >>> > > idea to keep a table handy, where the users are sorted with their id. >>> > > >>> > > -- >>> > > 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 >>> > > (mailto:nodejs@googlegroups.com) >>> > > To unsubscribe from this group, send email to >>> > > nodejs+unsubscr...@googlegroups.com >>> > > (mailto:nodejs+unsubscr...@googlegroups.com) >>> > > For more options, visit this group at >>> > > http://groups.google.com/group/nodejs?hl=en?hl=en >>> > >>> > >>> > >>> > -- >>> > 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 >>> > (mailto:nodejs@googlegroups.com) >>> > To unsubscribe from this group, send email to >>> > nodejs+unsubscr...@googlegroups.com >>> > (mailto: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 >>> (mailto:nodejs@googlegroups.com) >>> To unsubscribe from this group, send email to >>> nodejs+unsubscr...@googlegroups.com >>> (mailto: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 -- 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