From b2928a687e5f8cfb67abcca56f609c292edf1149 Mon Sep 17 00:00:00 2001 From: leopen <790480953@qq.com> Date: Sat, 27 Aug 2022 14:29:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E7=AE=80=E6=98=93pro?= =?UTF-8?q?mise=E4=BD=BF=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E9=80=9A?= =?UTF-8?q?=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 59d6c84..24a327c 100644 --- a/index.js +++ b/index.js @@ -8,15 +8,19 @@ function myPromise(constructor) { self.reason = undefined;//定义状态为rejected的时候的状态 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 +40,11 @@ function myPromise(constructor) { myPromise.prototype.then = function (onFullfilled, onRejected) { //TODO then如何实现 + if (this.status === "fulfilled" && onFullfilled) { + onFullfilled(this.value); + } else if (this.status === "rejectd" && onRejected) { + onRejected(this.reason); + } } module.exports = myPromise