function resolve(promise, x) {
    if (promise === x) {
      promise._reject(new TypeError('Cannot resolve a promise to itself'));
      return;
    }

    if (x instanceof AsyncHandle) {
      x.then(
        function(value) { resolve(promise, value); },
        function(reason) { promise._reject(reason); }
      );
      return;
    }

    var then;
    try {
      then = (/function|object/).test(typeof x) && x != null && x.then;
    } catch (e) {
      promise._reject(e);
      return;
    }

    var thenableState = PENDING;
    if (typeof then === 'function') {
      try {
        then.call(
          x,
          function resolvePromise(value) {
            if (thenableState !== PENDING) {
              return;
            }
            thenableState = RESOLVED;
            resolve(promise, value);
          },
          function rejectPromise(reason) {
            if (thenableState !== PENDING) {
              return;
            }
            thenableState = REJECTED;
            promise._reject(reason);
          }
        );
      } catch (e) {
        if (thenableState !== PENDING) {
          return;
        }

        promise._reject(e);
      }

      return;
    }

    promise._resolve(x);
  }
Promise resolution procedure:
http://promises-aplus.github.io/promises-spec/#the_promise_resolution_procedure