The trigger function appears to be synchronous. That is, all bound functions appear to be executed in sequence, synchronously (the invoked function may do something asynchronously, but that's not the question).
Is this true for custom and non-custom(click, hover, etc) events? Are there any events where the single threaded guarantee of Javascript does not hold true?
If it is true, can the behavior be changed to execute them asynchronously without inserting timeouts into each bound function?
Here is a sample which demonstrates The question:
var ele = $('#blah');
// Example of the bound function doing something synchronously
ele.bind('customEvent.sync', function() {
// Do something synchronously
window.foo = 'foo';
});
// Example of the bound function doing something asynchronously
ele.bind('customEvent.async', function() {
window.setTimeout(function() {
// Do something asynchronously
window.bar = 'bar';
}, 0);
});
// Trigger both events
ele.trigger('customEvent');
// If trigger is guaranteed to be synchronous this should alert 'foo:undefined' or possibly 'foo:bar' depending on whether the asych function was called first
// If trigger is NOT synchronous 'undefined:undefined', 'foo:undefined', 'undefined:bar', or 'foo:bar' could be alerted
alert(window.foo + ':' + window.bar);
UPDATE See: Is JavaScript guaranteed to be single-threaded? Custom events are guaranteed to be synchronous because of the single threaded nature of Javascript. Some built in event types may not be synchronous due to browser inconsistencies.