.</p>
<pre><code class="lang-js">// for the purpose of this example let&#39;s assume that variables `$q` and `okToGreet`
// are available in the current lexical scope (they could have been injected or passed in).

function asyncGreet(name) {
  var deferred = $q.defer();

  setTimeout(function() {
    deferred.notify(&#39;About to greet &#39; + name + &#39;.&#39;);

    if (okToGreet(name)) {
      deferred.resolve(&#39;Hello, &#39; + name + &#39;!&#39;);
    } else {
      deferred.reject(&#39;Greeting &#39