diff --git a/index.js b/index.js index 59d6c84..ebfcdbf 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,7 @@ +const pending = 'pending' +const fulfilled = 'fulfilled' +const rejected = 'rejected' + function myPromise(constructor) { let self = this; @@ -10,13 +14,20 @@ function myPromise(constructor) { function resolve(value) { // TODO resolve如何改变状态及返回结果 + if (self.status === pending) { + self.status = fulfilled + self.value = value + } } function reject(reason) { // TODO reject如何改变状态及返回结果 - + if (self.status === pending) { + self.status = rejected + self.reason = reason + } } //捕获构造异常 @@ -36,6 +47,8 @@ function myPromise(constructor) { myPromise.prototype.then = function (onFullfilled, onRejected) { //TODO then如何实现 - + if (this.status === fulfilled) onFullfilled(this.value) + + if (this.status === rejected) onRejected(this.reson) } module.exports = myPromise