<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title><![CDATA[RandomThink.net]]></title>
  <link href="http://www.randomthink.net/atom.xml" rel="self"/>
  <link href="http://www.randomthink.net/"/>
  <updated>2012-11-28T23:37:50-07:00</updated>
  <id>http://www.randomthink.net/</id>
  <author>
    <name><![CDATA[Brian Arnold]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
    <entry>
      




<title type="html"><![CDATA[Breakpoint Actions in JavaScript]]></title>
<link href="http://www.randomthink.net/blog/2012/11/breakpoint-actions-in-javascript/"/>
<updated>2012-11-28T22:48:00-07:00</updated>
<id>http://www.randomthink.net/blog/2012/11/breakpoint-actions-in-javascript</id>

      <content type="html"><![CDATA[<p>Earlier today, I filled out the <a href="https://docs.google.com/forms/d/1hhZwGQmtNeiRhPwxLurPaVElB0h_E6h_qjnzpxAXdtI/viewform">Chrome DevTools Survey &amp; Feedback</a>
(which you should too, if you&#8217;re a web developer), and near the bottom of the
survey, there was a question:</p>

<blockquote><p>Are there any tools or features you think are missing that would better help
you develop your webapps?</p></blockquote>

<p>My initial answer was No, there wasn&#8217;t anything I felt was missing. DevTools are
awesome, and there hasn&#8217;t really been anything I&#8217;ve felt like I needed.</p>

<p>Later in the day, I ran into a very common scenario: I was debugging something
where I wanted to observe a value at a particular line of code. There are
a couple of ways to approach it:</p>

<ul>
<li>Drop in a <code>debugger</code> statement or set a breakpoint to stop execution and use
the console and/or watch statements to observe the value</li>
<li>Inject some <code>console.log</code> statements to write the value out to the console</li>
</ul>


<p>In this case, the <code>console.log</code> approach would be ideal. However, I didn&#8217;t have
access to modify the code directly, and while I could have used the live
editing features, it was also minified code, meaning that live edit was an ugly
option.</p>

<p>Thinking back to the survey question, I now had an answer: Wouldn&#8217;t it be nice
if there was some way to have some sort of action kicked off, breakpoint-style,
where I&#8217;m not editing the code directly?</p>

<p>I asked some friends, and it turns out, other development environments have a
similar concept. Xcode has <a href="http://useyourloaf.com/blog/2011/02/21/xcode-breakpoint-actions.html">Breakpoint Actions</a> (as do some other IDEs) which
look to be exactly what I wanted. While bouncing this idea around, I realized
that I could actually fake it in the browser, right now.</p>

<p>Chrome supports conditional breakpoints. Adding them is easy. First, find the
line you want to set a breakpoint on, and click the line number to apply a
breakpoint like normal:</p>

<p><img class="" src="http://www.randomthink.net/images/blog/2012-11-28-breakpoint-actions/breakpoints-1.png" title="[Set a breakpoint]" ></p>

<p>Then you right-click on it, and select &#8220;Edit Breakpoint&#8221;:</p>

<p><img class="" src="http://www.randomthink.net/images/blog/2012-11-28-breakpoint-actions/breakpoints-2.png" title="[Edit Breakpoint]" ></p>

<p>You&#8217;re presented with your cursor in a field like so, where you can define an
expression. If the expression evaluates to a truthy value, execution will break
here.</p>

<p><img class="" src="http://www.randomthink.net/images/blog/2012-11-28-breakpoint-actions/breakpoints-3.png" title="[Set Condition]" ></p>

<p>It&#8217;s not uncommon to set simple expressions like <code>someObj.id === '1'</code>, but I
realized that <em>any</em> valid JS expression would be evaluated any time this line
was hit &#8211; including the <code>console.log</code> statement I wanted to add!</p>

<p><img class="" src="http://www.randomthink.net/images/blog/2012-11-28-breakpoint-actions/breakpoints-4.png" title="[console.log dirtiness]" ></p>

<p>This approach works quite well. Looking over at the console after moving my
mouse around a bit on this page (which happens to be the <a href="https://developers.google.com/chrome-developer-tools/docs/scripts-breakpoints">Chrome DevTools
Breakpoints page</a>), you can clearly see the added
<code>console.log</code> firing:</p>

<p><img class="" src="http://www.randomthink.net/images/blog/2012-11-28-breakpoint-actions/breakpoints-5.png" title="[console.log is working!]" ></p>

<p>Since <code>console.log</code> doesn&#8217;t have a meaningful return value, the <code>undefined</code>
means the conditional breakpoint won&#8217;t pause execution, and the code moves on.
It&#8217;s just like a coded <code>console.log</code> statement, without having to actually
modify the code.</p>

<p>Using a conditional breakpoint like this feels horribly hacky, and yet, I love
this idea. It achieves the exact goal that I wanted. It&#8217;s not elegant, it&#8217;s not
by design, but it gets the job done, like so many other things in web
development.</p>

<p>As an added note, Firebug supports conditional breakpoints in much the same
way, and this approach totally works there as well. In both cases, it&#8217;s working
simply because you&#8217;re just evaluating the expression, and only pausing
execution if it evaluates to a truthy value.</p>

<p>Hopefully you&#8217;ll find this useful at some point! I already have.</p>
<p><a rel="bookmark" href="http://www.randomthink.net/blog/2012/11/breakpoint-actions-in-javascript/">&#9875; Permalink</a></p>]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Callbacks and Promises]]></title>
<link href="http://www.randomthink.net/blog/2012/10/callbacks-and-promises/"/>
<updated>2012-10-18T21:36:00-06:00</updated>
<id>http://www.randomthink.net/blog/2012/10/callbacks-and-promises</id>

      <content type="html"><![CDATA[<p>Anyone who&#8217;s written even a little bit of JavaScript is likely familiar with the
callback pattern. Even if you don&#8217;t know it by that name, you&#8217;ve probably
written code using it. When calling a function, we&#8217;ll pass it another function
as an argument, which will be used to let us know that something has happened.
In particular, callbacks are frequently used for responding to things that
happen asynchronously.</p>

<p>For example, event handling is based on this pattern. We take a DOM node and
provide it with a callback function so that when the associated event occurs on
that node, it has a way of letting us know what&#8217;s going on.</p>

<!-- more -->




<figure class='code'><figcaption><span>Simple document-based click handler</span></figcaption><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nb">document</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="s2">&quot;click&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">){</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;Document was clicked!&quot;</span><span class="p">);</span>
</span><span class='line'><span class="p">},</span> <span class="kc">false</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>This sort of code is very commonplace, and is one of the more powerful features
of JavaScript. When using code that accepts callbacks, we don&#8217;t usually think
too much of it.</p>

<p>As we create richer applications, we&#8217;ll often need to create functions that pass
information back to our users. If the information we&#8217;re passing back comes from
asynchronous sources, it may be beneficial to create functions that that accept
callbacks. Because JavaScript functions are <a href="http://en.wikipedia.org/wiki/First-class_function#Higher-order_functions:_passing_functions_as_arguments">first-class objects</a>, you can
receive them as arguments, and then simply invoke them.</p>

<p>I prefer <a href="http://dojotoolkit.org/">Dojo</a> these days, but for simplicity&#8217;s sake, I&#8217;ll show an example
using <a href="http://jquery.com/">jQuery</a>, as I suspect that will be more familiar to most people.</p>

<figure class='code'><figcaption><span>Simple function that accepts a callback</span></figcaption><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">getManagers</span><span class="p">(</span><span class="nx">callback</span><span class="p">){</span>
</span><span class='line'>  <span class="c1">// Kick off our Ajax request for information.</span>
</span><span class='line'>  <span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">({</span>
</span><span class='line'>    <span class="nx">url</span><span class="o">:</span> <span class="s2">&quot;/company/allEmployees.json&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">dataType</span><span class="o">:</span> <span class="s2">&quot;json&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">success</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">// Using our callback, provide manager data to our user.</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">managers</span> <span class="o">&amp;&amp;</span> <span class="k">typeof</span> <span class="nx">callback</span> <span class="o">==</span> <span class="s2">&quot;function&quot;</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">callback</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">managers</span><span class="p">);</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice that in this case, I&#8217;m not returning anything from the <code>getManagers</code>
function. I&#8217;m communicating with my user by invoking the callback they&#8217;ve
provided to me. This approach is very effective, and people are generally
comfortable with using callback-driven functions, so it&#8217;s a solid and perfectly
valid way to write code. It could also be extended to handle not just a callback
for success states, but another one for errors.</p>

<figure class='code'><figcaption><span>Improved version with callback/errback support</span></figcaption><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">getManagers</span><span class="p">(</span><span class="nx">callback</span><span class="p">,</span> <span class="nx">errback</span><span class="p">){</span>
</span><span class='line'>  <span class="c1">// Kick off our Ajax request for information</span>
</span><span class='line'>  <span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">({</span>
</span><span class='line'>    <span class="nx">url</span><span class="o">:</span> <span class="s2">&quot;/company/allEmployees.json&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">dataType</span><span class="o">:</span> <span class="s2">&quot;json&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">success</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">// We&#39;re breaking the logic to check just for managers first,</span>
</span><span class='line'>      <span class="c1">// in order to better facilitate error handling.</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">managers</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="c1">// We have our data, so invoke our callback.</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">callback</span> <span class="o">==</span> <span class="s2">&quot;function&quot;</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="nx">callback</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">managers</span><span class="p">);</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>      <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">errback</span> <span class="o">==</span> <span class="s2">&quot;function&quot;</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="c1">// We have no manager data, so if we have an errback,</span>
</span><span class='line'>        <span class="c1">// give it something meaningful to indicate failure.</span>
</span><span class='line'>        <span class="nx">errback</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s2">&quot;No managers found&quot;</span><span class="p">));</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>    <span class="p">},</span>
</span><span class='line'>    <span class="nx">error</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">jqXHR</span><span class="p">,</span> <span class="nx">textStatus</span><span class="p">,</span> <span class="nx">errorThrown</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">// If we have the errback, just give it the error we got.</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">errback</span> <span class="o">==</span> <span class="s2">&quot;function&quot;</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">errback</span><span class="p">(</span><span class="nx">errorThrown</span><span class="p">);</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Using this function is actually quite easy:</p>

<figure class='code'><figcaption><span>Using getManagers</span></figcaption><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="c1">// Simply call it and provide your callback.</span>
</span><span class='line'><span class="c1">// In this case, I&#39;m not going to worry about errors.</span>
</span><span class='line'><span class="nx">getManagers</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">managers</span><span class="p">){</span>
</span><span class='line'>  <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">managers</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// Do something with each manager here</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Writing code that accepts callbacks and uses them is really simple. That being
said, callbacks can lead to tightly coupled code. It means that I have to know
how I want to process my information at the point of retrieving it. It makes it
more difficult to write reusable abstractions of the process used to request
data.</p>

<p>Fortunately, there is another approach available to us. Modern JavaScript
libraries and toolkits offer us implementations of a concept known as promises.
In general terms, a promise is simply an object that represents the value of an
asynchronous action. It&#8217;s kind of like an IOU for information. It also happens
to fit the above scenario quite well.</p>

<p>If you&#8217;re using Dojo or jQuery, you already have promises available to you. You
can create a Deferred object, which is the mechanism for creating and managing
promises.</p>

<p>Working with Deferreds is quite easy. You simply create an instance of the
Deferred constructor, and then when your value is ready, you <code>resolve</code> the
instance with the value that it represents. If you hit an error state, you can
<code>reject</code> the instance. Let&#8217;s take the above example and rework it to be using
promises instead of callbacks.</p>

<figure class='code'><figcaption><span>Getting managers via Deferreds / promises</span></figcaption><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">getManagers</span><span class="p">(){</span>
</span><span class='line'>  <span class="c1">// Create our Deferred instance</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">deferred</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">$</span><span class="p">.</span><span class="nx">Deferred</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">// Then, we&#39;ll perform our Ajax request like before.</span>
</span><span class='line'>  <span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">({</span>
</span><span class='line'>    <span class="nx">url</span><span class="o">:</span> <span class="s2">&quot;/company/allEmployees.json&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">dataType</span><span class="o">:</span> <span class="s2">&quot;json&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">success</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">// This time though, we&#39;ll simply resolve or reject</span>
</span><span class='line'>      <span class="c1">// our Deferred instances.</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">managers</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">deferred</span><span class="p">.</span><span class="nx">resolve</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">managers</span><span class="p">);</span>
</span><span class='line'>      <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">deferred</span><span class="p">.</span><span class="nx">reject</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s2">&quot;No managers found&quot;</span><span class="p">));</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>    <span class="p">},</span>
</span><span class='line'>    <span class="nx">error</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">jqXHR</span><span class="p">,</span> <span class="nx">textStatus</span><span class="p">,</span> <span class="nx">errorThrown</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="nx">deferred</span><span class="p">.</span><span class="nx">reject</span><span class="p">(</span><span class="nx">errorThrown</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">// Give our user the promise object that represents</span>
</span><span class='line'>  <span class="c1">// our managers.</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">deferred</span><span class="p">.</span><span class="nx">promise</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>One key difference above is that we&#8217;re actively providing a return value now,
whereas in the callback-based version, we didn&#8217;t. This object that we&#8217;re
returning is the promise that we&#8217;re giving to our user, and it provides them
with the ability to get the information we&#8217;re promising, once that data is
available.</p>

<p>The Deferred is the mechanism by which I manage the promise. It gives me the
ability to create a promise that I provide to my end user, as well as the
ability to then fulfill that promise in a meaningful way.</p>

<p>This approach almost feels synchronous. I create an object at the top of my
function, and I return it at the bottom. The user doesn&#8217;t give me a callback,
but I give the user a meaningful object.</p>

<p>The promise object gives your user a <code>then</code> method, which they can then use to
attach a callback and get access to the value, like so:</p>

<figure class='code'><figcaption><span>Using the return value from getManagers as a promise</span></figcaption><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">getManagers</span><span class="p">().</span><span class="nx">then</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">managers</span><span class="p">){</span>
</span><span class='line'>  <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">managers</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// Do something with each manager here,</span>
</span><span class='line'>    <span class="c1">// such as render a list item.</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>There is still a callback in action here, but now it&#8217;s being managed in the code
that is consuming the information, rather than in the code that is providing it.
We&#8217;ve effectively decoupled the process of retrieving information from the
process of consuming it, which is a huge win for creating reusable code.</p>

<p>There&#8217;s also something to be said about reading promise-based code. It feels
very natural. I take this action, <code>then</code> I take this other action. It can reduce
excessive amounts of indentation, especially when doing a lot of Ajax work at
once.</p>

<p>In fact, this approach is so powerful that both jQuery and Dojo already provide
a promise as the return value of their Ajax systems. Dojo 1.8&#8217;s new
<code>dojo/request</code> module is explicitly promise-driven, so requests look like this:</p>

<figure class='code'><figcaption><span>Using dojo/request to perform a request</span></figcaption><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">require</span><span class="p">([</span><span class="s2">&quot;dojo/request&quot;</span><span class="p">],</span> <span class="kd">function</span><span class="p">(</span><span class="nx">request</span><span class="p">){</span>
</span><span class='line'>  <span class="nx">request</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">&quot;/company/allEmployees.json&quot;</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">handleAs</span><span class="o">:</span> <span class="s2">&quot;json&quot;</span>
</span><span class='line'>  <span class="p">}).</span><span class="nx">then</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">){</span>
</span><span class='line'>    <span class="c1">// Work with your data here</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>When creating your own modules of code to abstract away the process of data
retrieval from data consumption, both callbacks and promises are valid &#8211;
there&#8217;s not one right way to do it. However, using a promise means that you&#8217;re
giving your users back a meaningful object, and how they deal with the
information is up to them. It leads to loosely coupled code, which in turn leads
to better reuse and modularization.</p>

<p>Hopefully you&#8217;ll consider using promises in your next application! I&#8217;ve barely
scratched the surface of what makes them awesome. Check out the links below for
much more information on promises and Deferreds. Thanks for reading!</p>

<ul>
<li>Dojo:

<ul>
<li><a href="http://dojotoolkit.org/documentation/tutorials/1.8/deferreds/">Getting Started with Deferreds</a></li>
<li><a href="http://dojotoolkit.org/documentation/tutorials/1.8/promises/">Dojo Deferreds and Promises</a></li>
</ul>
</li>
<li>jQuery:

<ul>
<li><a href="http://api.jquery.com/category/deferred-object/">Deferred Object</a></li>
</ul>
</li>
<li><a href="http://wiki.commonjs.org/wiki/Promises/A">Promises/A specification</a></li>
</ul>

<p><a rel="bookmark" href="http://www.randomthink.net/blog/2012/10/callbacks-and-promises/">&#9875; Permalink</a></p>]]></content>
    </entry>
  
</feed>
