title: How to execute code at the very end of a promise chain in q
body: <p>Let's say you have:</p>
  
  <pre><code>function setTimeoutPromise(ms) {
    var defer = Q.defer();
    setTimeout(defer.resolve, ms);
    return defer.promise;
  }
  </code></pre>
  
  <p>and then you have something like:</p>
  
  <pre><code>function foo(ms) {
    return setTimeoutPromise(ms).then(function () {
      console.log('foo');
    });
  }
  
  function afterItAll() {
    console.log('after it all');
  }
  
  foo(100).then(function () {
    console.log('after foo');
  }).then(afterItAll);
  </code></pre>
  
  <p>Is there a way to modify <em>foo</em> so that <em>afterItAll</em> is executed after the <em>after foo</em> block? e.g. something like:</p>
  
  <pre><code>function foo(ms) {
    return setTimeoutPromise(ms).then(function () {
      console.log('foo');
    }).SOMEPROMISECONSTRUCT(function () {
      console.log('after it all');
    });
  }
  
  foo(100).then(function () {
    console.log('after foo');
  });
  </code></pre>
  
  <p>The reason I ask is that I am developing an API where the user will make several of these <em>foo</em> calls and it would greatly cut down on the user's code if the <em>after foo</em> code was automatically executed after these API calls. I know I can use a callback to accomplish this, but I'd really like to stick with just using promises.</p>