{"size": 2287, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "rencoo/Promise", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "rencoo/Promise", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "rencoo/Promise", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.5773195876, "max_line_length": 80, "alphanum_fraction": 0.5714910363}
class MyPromise {
  constructor(executor) {
    this._state = 'pending'
    this._result = undefined

    // 并发
    this._resolveHandlers = []
    this._rejectHandlers = []

    const resolve = (response) => {
      if (this._state === 'pending') {
        this._state = 'fulfilled'
        this._result = response

        this._resolveHandlers.forEach((handler) => {
          handler(response)
        })
      }
    }
    const reject = (error) => {
      if (this._state === 'pending') {
        this._state = 'rejected'
        this._result = error

        this._rejectHandlers.forEach((handler) => {
          handler(error)
        })
      }
    }

    executor(resolve, reject)
  }

  then(resolveHandler, rejectHandler) {
    if (this._state === 'pending') {
      return new MyPromise((resolve, reject) => {
        // 这里既实现了resolveHandler的注册，同时也能将resolveHandler的执行结果留在新promise里再resolve出去
        const _resolveHandler = (value) => {
          const result = resolveHandler(value)
          // resolve(result)
          resolvePromise(result, resolve, reject)
        }
        this._resolveHandlers.push(_resolveHandler)

        const _rejectHandler = (error) => {
          const result = rejectHandler(error)
          reject(result)
        }
        this._rejectHandlers.push(_rejectHandler)
      })
    }

    if (this._state === 'fulfilled') {
      return new MyPromise((resolve, reject) => {
        const result = resolveHandler(this._result)
        resolve(result)
      })
    }

    if (this._state === 'rejected') {
      return new MyPromise((resolve, reject) => {
        const result = rejectHandler(this._result)
        resolve(result)
      })
    }
  }

  catch(handler) {}

  finally(handler) {}
}

// const resolvePromise = (result, resolve, reject) => {
//   if (result instanceof MyPromise) {
//     if (result._state === 'pending') {
//       result.then(function (data) {
//         resolvePromise(data, resolve, reject)
//       })
//     } else {
//       result.then(resolve, reject)
//     }
//     return
//   }

//   return resolve(result)
// }

const resolvePromise = (result, resolve, reject) => {
  if (result instanceof MyPromise) {
    result.then(resolve, reject);
  } else {
    resolve(result);
  }
};

module.exports = MyPromise
