I found myself in need of some advanced input validation for a project I'm working on. Some of the validation happens synchronously, while other validation happens asynchronously. The Deferred framework is useful, but unfortunately I need to handle the synchronous and asynchronous validation in separate locations.
I like how you can drop non-deferred objects into $.when(...). However, the result is always a pass and even if it weren't there's no convenient way to pass extra data to the success or failure handlers.
Therefore, I propose a new method "$.defer(...)" that will create a quick deferred object that will immediately succeed or fail based on the value passed as the first argument. It also takes a second argument that will be passed on success (and failure if no third argument is defined), and a third argument that will be passed on failure. If the second and third arguments are not defined, the result of the first argument will be passed to the success or failure handler (whichever gets called).
I implemented the additional method and created a bunch of examples here:
I'd like to hear thoughts on the system. Am I barking up the wrong tree? Is there a better way to do what I'm doing? Would this be useful to others? Are there better ways to implement the change? How about performing the same operation by passing arguments to $.Deferred()?
Thanks!