Could jQuery.Callbacks fire() play safer when calling up the stacked callbacks?

Could jQuery.Callbacks fire() play safer when calling up the stacked callbacks?

Hello there,

I had an issue which made me debug up to jQuery in order to figure out what was the issue. And with that, got me thinking about the current fire() implementation ( see it here:  https://github.com/jquery/jquery/blob/1.x-master/src/callbacks.js#L72 ).

Sorry for the long text ;P

We use requirejs for module loading, therefore we might end with each module having it's own doc.ready trigger. No biggie as jQuery pile up the callbacks in an array and do a for(). Using requirejs means async loading and then, the ready callbacks are queued at different order.

Consider the following fiddle (pretend each ready is on a different file):  http://jsfiddle.net/RaphaelDDL/e7x4azgd/
  1. var logger = '#log';

  2. //file1
  3. $(function(){
  4.     $('<li/>').text('ready 1').appendTo(logger);
  5. });
  6. //file2
  7. $(function(){
  8.     $('<li/>').text('ready 2').appendTo(logger);
  9. });
  10. //file3
  11. $(function(){
  12.     $('<li/>').text('ready 3').appendTo(logger);
  13. });
  14. //file4
  15. $(function(){
  16.     $('<li/>').text('ready 4').appendTo(logger);
  17. });
  18. //file5
  19. $(function(){
  20.     $('<li/>').text('ready 5').appendTo(logger);
  21. });

On fire(), list will be an array with 5 functions. Each will be called by  list[ firingIndex ]. apply ( data[ 0 ], data[ 1 ] ).

So, the issue was that one of the callbacks on one of the modules of another team was wrong and had a TypeError. So the callback for some of the ready() were never been called.

Consider this fiddle (Error on callback #3):  http://jsfiddle.net/RaphaelDDL/e7x4azgd/3/
  1. var logger = '#log';

  2. //file1
  3. $(function(){
  4.     $('<li/>').text('ready 1').appendTo(logger);
  5. });
  6. //file2
  7. $(function(){
  8.     $('<li/>').text('ready 2').appendTo(logger);
  9. });
  10. //file3
  11. $(function(){
  12.     $('<li/>').text('ready 3'). typeErrorFn(logger);
  13. });
  14. //file4
  15. $(function(){
  16.     $('<li/>').text('ready 4').appendTo(logger);
  17. });
  18. //file5
  19. $(function(){
  20.     $('<li/>').text('ready 5').appendTo(logger);
  21. });

Now the title's question:
Could jQuery.Callbacks fire() play safer when calling up the stacked callbacks?

I mean, could  list[ firingIndex ]. apply ( data[ 0 ], data[ 1 ] ) be wrapped on a try/catch, where catch pushes any errors to an errorArray and in the end of the for() then the errors are thrown or console.logged? That is, can jQuery's fire() be decoupled of third-party code errors (a.k.a. us who use jQ) in order to not halt jQuery's own execution of callbacks?

Thinking on different .js files/code blocks, inside their own closure one does not halt another execution (if they are unrelated or don't call one another). But in this case where all come into jQuery's fire(), one error halts all jQ execution.

I guess was explained okay. But if not, let me know, I'll gladly explain better any part of the idea.

Regards,
Raphael

PS: Yes, would be best just fix the typeError, but sometimes the module/code is from another team so you can't much fix it by yourself, you gotta report and etc. Bureaucracy, you know. But the main point here is jQ flow breaking 'cause of bad callback.