Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock promise #15

Open
chiyan-lin opened this issue May 17, 2020 · 0 comments
Open

mock promise #15

chiyan-lin opened this issue May 17, 2020 · 0 comments

Comments

@chiyan-lin
Copy link
Owner

class Promises {
  // 三种状态 pending(进行中)、fulfilled(已成功)和rejected(已失败)

  /**
     * 常用方式Promise.resolve(something).then(...)
     * 可见Promise.resolve返回一个新的Promise实例
     **/
  static resolve() { }

  static reject() { }

  status = 'pending'
  callback = () => { }

  constructor(fn) {
    fn(this._resolve.bind(this), this._reject)
  }

  _resolve(value) {
    this.callback(value)
  }

  _reject() {

  }

  then(fn) {
    this.callback = fn
  }
}

var aa = new Promises((resolve, reject) => {
  setTimeout(() => {
    resolve(1)
  }, 2000)
})

aa.then((value) => {
  console.log('value', value, this)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant