Just a quick note on jQuery Deferred objects…
They’re basically a callback wrapper. This example is a bit redundant since jQuery $.ajax returns a Deferred already, but you get the idea. You can use this for anything that requires a callback. It provides a pattern that pushes the callback function into user code in a way that is standardized. The resolve and reject functions on deferred pass their parameters into the callbacks in the done/fail functions.
This lets you pass the deferred promise around (a promise is just a deferred with the resolve/reject removed from its interface). A bit cleaner functional approach that removes the need for you to worry about call/apply syntax and context.
The above can then be used like this: