lib/ipromise/core.js
function doResolve(result) {
  let self = this;
  if (result === self) {
    // Promise Standard 2.3.1
    return doReject.call(
      self,
      new TypeError("Can not resolve 'Promise' itself")
    );
  }
  // Promise Standard 2.3.3.2
  try {
    // Promise Standard 2.3.2 and 2.3.3 can be merge
    // if result is a thenable(.then)
    let then = safelyToThen(result);
    if (then) {
      // Promise Standard 2.3.3.3
      safelyToExecutor(self, then.bind(result));
    } else {
      // [[ async ]]
      setTimeout(function() {
        // commonly case
        if (self.currentState === Shared.PENDING) {
          self.currentState = Shared.FULFILLED;
          self.valOrErr = result;
          for (let i = 0; i < self.onFulfilledCallback.length; i++) {
            self.onFulfilledCallback[i](result);
          }
          self.onFulfilledCallback = [];
        }
      }, 0);
    }
    // case: when then(doResolve, doReject) -> asyncToResolveOrReject -> func(arg) -> return self
    return self;
  } catch (error) {
    return doReject.call(self, error);
  }
}
function doResolve(result) {
  let self = this;
  if (result === self) {
    // Promise Standard 2.3.1
    return doReject.call(
      self,
      new TypeError("Can not resolve 'Promise' itself")
    );
  }
  // Promise Standard 2.3.3.2
  try {
    // Promise Standard 2.3.2 and 2.3.3 can be merge
    // if result is a thenable(.then)
    let then = safelyToThen(result);
    if (then) {
      // Promise Standard 2.3.3.3
      safelyToExecutor(self, then.bind(result));
    } else {
      // [[ async ]]
      setTimeout(function() {
        // commonly case
        if (self.currentState === Shared.PENDING) {
          self.currentState = Shared.FULFILLED;
          self.valOrErr = result;
          for (let i = 0; i < self.onFulfilledCallback.length; i++) {
            self.onFulfilledCallback[i](result);
          }
          self.onFulfilledCallback = [];
        }
      }, 0);
    }
    // case: when then(doResolve, doReject) -> asyncToResolveOrReject -> func(arg) -> return self
    return self;
  } catch (error) {
    return doReject.call(self, error);
  }
}
function doResolve ( result ) { let self = this ; if ( result === self ) { // Promise Standard 2.3.1 return doReject . call ( self , new TypeError ( "Can not resolve 'Promise' itself" ) ) ; } // Promise Standard 2.3.3.2 try { // Promise Standard 2.3.2 and 2.3.3 can be merge // if result is a thenable(.then) let then = safelyToThen ( result ) ; if ( then ) { // Promise Standard 2.3.3.3 safelyToExecutor ( self , then . bind ( result ) ) ; } else { // [[ async ]] setTimeout ( function ( ) { // commonly case if ( self . currentState === Shared . PENDING ) { self . currentState = Shared . FULFILLED ; self . valOrErr = result ; for ( let i = 0 ; i < self . onFulfilledCallback . length ; i ++ ) { self . onFulfilledCallback [ i ] ( result ) ; } self . onFulfilledCallback = [ ] ; } } , 0 ) ; } // case: when then(doResolve, doReject) -> asyncToResolveOrReject -> func(arg) -> return self return self ; } catch ( error ) { return doReject . call ( self , error ) ; } }
Safely exec executor then doResolve or doReject
The most important function call
Safely exec executor then doResolve or doReject The most important function call