Wilsonhuthttps://wilsonhut.wordpress.com
Deal with it or don'tFri, 21 Jul 2017 15:54:25 +0000enhourly1http://wordpress.com/https://s2.wp.com/i/buttonw-com.pngWilsonhuthttps://wilsonhut.wordpress.com
$.gasp is published!https://wilsonhut.wordpress.com/2013/06/21/gasp-is-published/
https://wilsonhut.wordpress.com/2013/06/21/gasp-is-published/#respondFri, 21 Jun 2013 21:32:04 +0000http://wilsonhut.wordpress.com/?p=582]]>jQuery Gasp is published. It’s almost as if they let just anyone on the internet. Am I right?]]>https://wilsonhut.wordpress.com/2013/06/21/gasp-is-published/feed/0wilsonhut$.defer renamed to $.whereas, and publishedhttps://wilsonhut.wordpress.com/2013/06/21/defer-renamed-to-whereas/
https://wilsonhut.wordpress.com/2013/06/21/defer-renamed-to-whereas/#respondFri, 21 Jun 2013 21:30:31 +0000http://wilsonhut.wordpress.com/?p=580]]>Only because the name $.defer was already taken.

Now that doesn’t get you much, except the ability to use the .then/.done methods when you call initializeTheDifficultStuff(), since the ‘difficult stuff’ is not asyncronous. If the ‘difficult stuff’ was an ajax call, and you put the d.resolve() in its callback, then you’d have something.

I discussed all of this in a previous post, but now I want to take it a step further…

What if you want to use the progress callback to show a ‘please wait…’ message?

What if you want to use the above method like this? (Here, I’m using the $.blockUI plug-in)

The reason you might WANT to do that is so that you could keep the $.blockUI call from happening if the “isInitialized” is already set. That’s what you want, since it’s not going to have to do the hard work in that case, and the $.blockUI call starts a fade-in which would be unnecessary.

Now that gets more difficult with the above implementation of initializeTheDifficultStuff, because, unlike the done and then, events, the progress is never called on a pre-resolved promise. So what can you do? You can return the d.promise, like above, but just before that, put a call to d.notify and d.resolve in a setTimeout. That would start to look ugly real fast. I’m not even going to show that.

$.defer handles this all for you.

You can use the initializeTheDifficultStuff() with the progress and done events, and have clean looking code by using $.defer, like so:

The $.when() returns a resolved promise, so your progress event handler doesn’t get called!$.defer returns an un-resolved promise. And in this example, it would wait the minimum number of milliseconds, then do the magic. Here’s what it will do for you…
It will first cause your progress event handler to get called. Then it runs the ‘difficult stuff’ for you. Then it is resolved so that your done event handler gets called.

It’s all so pretty and neat, and you get the progress event only when you need it!

In later posts, I’ll show you some other cool things you can do with this other than give your browser a little relief. (Spoiler: Remember that $(…) can take things other than a selector string as an argument.)

“Too bad you can’t use window.setTimout and have it implement the jQuery Deferred interface.”

We’re all upset about that. So…

“Wait. What? Now you can?!?”

Right. Now you can. I searched the internet and couldn’t find an existing one, to my surprise. I wrote it for you though.

You can skip right to the Code below, skip to a jsfiddle sample, or keep reading for more explanation.

It’s almost as simple as my sample code in my last post explaining the basics of Deferred with the addition of an actual setTimeout… Make a $.Deferred. Call the provided func in a setTimeout, using the provided delay and parameters. After the call to the provided func, call .resolve, and in a catch block, call .reject.

You can use it without the function parameter to return a promise that will resolve in [delay] milliseconds

return $.defer(100);

The delay is optional too. The default delay of 0 (zero) will be used.

return $.defer(function(){alert("ASYNC!");});

or

return $.defer();

“You had me at ‘Promise’, now shut up and give me the code”

Here it is. It’s a little longer than the way I explained it above, but that’s just because it has the code for cancelling, and I like for the actually code to look long and complex to make me look smarter.

]]>https://wilsonhut.wordpress.com/2013/05/30/jquery-deferred-ified-window-settimeout/feed/10wilsonhutThe simplest explanation of jQuery Deferredhttps://wilsonhut.wordpress.com/2013/05/30/the-simplest-explanation-of-jquery-deferred/
https://wilsonhut.wordpress.com/2013/05/30/the-simplest-explanation-of-jquery-deferred/#commentsThu, 30 May 2013 18:55:52 +0000http://wilsonhut.wordpress.com/?p=518]]>I spent a while trying to get to the bottom of the question, “What is the jQuery Deferred all about?”

I couldn’t find the simple answer (They all seem to include ajax examples), so I decided that the internet needed a very short post about the basics.

Deferred is all about callbacks

Let’s say you are the developer of some asynchronous method. You need to provide a way to callback to the consuming code so that it can be aware of success and failure. One way to do this is to make use of a $.Deferred().

As the author of the method, you would create a new $.Deferred(), and use that* as the return value of the function.
When the real meat of your code has completed, you call the deferred’s .resolve method, and if the meat of your code fails, you call the deferred’s .reject method.

When you call .resolve or .reject, all of the appropriate callbacks that the consumer set up get called.

As the basics, that’s it. Make a Deferred. Call resolve or reject, then the done or fail callbacks get called.

That’s a bad way to use it because nothing there is asynchronous, but that’s the simplicity that I felt the internet was missing.

* Really, you should return deferred.promise()

So what is the difference between a Deferred and a Promise?

The promise is just like returning the Deferred, but with the promise, the consuming code doesn’t have access to the .resolve .reject, and other methods that CHANGE the state. Using the promise, the consuming code only has access to the .done, .fail, etc.

I was writing some extension methods for things such as Aggregate where the only purpose was to pass in default values. That was not very smart, since you can always use DefaultIfEmpty() and pass your default value there. Really, what I needed was an overload of DefaultIfEmpty that takes a Func to get the default value, in case the default value is expensive (a database look-up, for example)