diff --git a/index.js b/index.js index 59d6c84..afd2196 100644 --- a/index.js +++ b/index.js @@ -8,15 +8,14 @@ function myPromise(constructor) { self.reason = undefined;//定义状态为rejected的时候的状态 function resolve(value) { - - // TODO resolve如何改变状态及返回结果 + self.status = 'resolve'; + self.value = value; } function reject(reason) { - - // TODO reject如何改变状态及返回结果 - + self.status = 'reject'; + self.reason = reason; } //捕获构造异常 @@ -34,8 +33,11 @@ function myPromise(constructor) { } myPromise.prototype.then = function (onFullfilled, onRejected) { - - //TODO then如何实现 + if (this.status === 'resolve') { + onFullfilled(this.value); + } else if (this.status === 'reject') { + onRejected(this.reason); + } } module.exports = myPromise