Reputation: 11602
In my code, I want to test if a function is asynchronous, and if it is, use it's callback function. But if not, then I'll just call the next function manually. So is there a way to test if a function is asynchronous? And if not, then where could I find a list of all asynchronous jQuery functions?
if (isAsynchronous(fn)) {
args.push(function () { alert('callback!'); });
this.fn.apply(this, args);
} else {
this.fn.apply(this, args);
alert('synchronous');
}
So if that is not possible, having a list would be the next best thing.
var asynchs = {
animate: true,
fadeIn: true
//etc...
};
if (asynchs[fn]) {
args.push(function () {alert('callback!'); });
this.fn.apply(this, args);
} else {
this.fn.apply(this, args);
alert('synchronous');
}
Upvotes: 1
Views: 359
Reputation: 707198
There is no programmatic way to determine if a function is synchronous or asynchronous. In fact, it's possible for a function to be either synchronous or asynchronous depending upon what it's passed. Like $.ajax()
can be passed a parameter that determines which it is.
So, if you want to know what a function is, you have to either read the doc or the code.
In general, if a function offers a "completion" callback or something like that, then you should suspect that it's probably asynchronous or has an asynchronous option and look in the documentation to confirm. If a jQuery method does not have a completion callback, then it's probably not asynchronous.
Other things like $.each()
just use callbacks as part of their implementation (they are not "completion" callbacks) and are not asynchronous.
You should also suspect that ANY method that does animation or networking or waits some period of time is probably asynchronous. The jquery doc is really well written so it's usually pretty trivial to take a quick look if you're unsure.
Upvotes: 2