In my understanding the problem is not that it's not possible to prevent callback from being called twice, with underscore it's as simple as `cb = _(cb).once()` - but the problem is that it's cumbersome to do it everywhere.
On Wednesday, 20 August 2014 00:18:30 UTC+4, Simon Doodkin wrote: > > Not long ago, T.J. complained in an article that he doesn't likes that in > node.js sometimes callbacks are called more than once. > i have a simple solution for this if you need it. also i can suggest that > if you have callback problems you could use > https://github.com/mattinsler/longjohn module to trace the problem. id > did it several times. > > cbguard: https://gist.github.com/shimondoodkin/a6762d8ab29ea497e245 > > // cbguard by Shimon Doodkin - license: public domain > > function cbguard(cb,printerr){ //kind of filter for callbacks. it prevents a > callback to be called twice > var cb1=cb; > return function() { > if(cb1) { var cb2=cb1; cb1=false; return cb2.apply(this,arguments); } > else if(printerr)console.log(new Error('cb called twice').stack); > } > } > > // usage example: > //function myfunction(cb) > //{ > // cb=cbguard(cb); // usage example > // // or > // // cb=cbguard(cb,true); // good when developing, it will protect and also > tell you you have a problem, here a callback is called twice here. > // > // things that are based on event emitteres are usually have problems with > multiple callbacks. > > > -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: 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 unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/0bfddd86-2dfd-4e35-a3a4-72c323e739b7%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
