Unrelated Promise Resolution Order Guarantees
<p>I am wondering whether the following is defined behavior per the Promise specification:</p>

<pre><code>var H = function (c) {
  this.d_p = Promise.resolve();
  this.d_c = c;
};

H.prototype.q = function () {
  var s = this;
  return new Promise(function (resolve) {
    s.d_p = s.d_p.then(function () {       // (1)
      s.d_c({
        resolve: resolve
      });
    });
  });
};

var a,
    h = new H(function (args) { a = args; }),
    p;

Promise.resolve()
.then(function () {
  p = h.q();
})
.then(function () {                        // (2)
  a.resolve(42);
  return p;
});
</code></pre>

<p>The question is whether it's guaranteed that the <code>then</code> callback marked <code>(1)</code> is called before the <code>then</code> callback marked <code>(2)</code>.</p>

<p>Note that both promises in question are instantly resolved, so it seems to me like the <code>(1)</code> then callback should be scheduled as part of calling <code>h.q()</code>, which should be before the promise used to resolve <code>(2)</code> is resolved, so it should be before <code>(2)</code> is scheduled.</p>

<p>An example jsfiddle to play about with: <a href="https://jsfiddle.net/m4ruec7o/" rel="nofollow">https://jsfiddle.net/m4ruec7o/</a></p>

<p>It seems that this is what happens with bluebird >= 2.4.1, but not prior versions.  I tracked the change in behavior down to this commit: <a href="https://github.com/petkaantonov/bluebird/commit/6bbb3648edb17865a6ad89a694a3241f38b7f86e" rel="nofollow">https://github.com/petkaantonov/bluebird/commit/6bbb3648edb17865a6ad89a694a3241f38b7f86e</a></p>

<p>Thanks!</p>