From 7d1361edbf377d05e91d1e905a514d8dbd74604a Mon Sep 17 00:00:00 2001 From: blankzz <454257312@qq.com> Date: Sun, 21 Aug 2022 00:18:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(exercise3):=20=E5=AE=8C=E6=88=90=20Exe?= =?UTF-8?q?rcise3=20-=20=E6=89=8B=E5=86=99=E7=AE=80=E6=98=93=20Promise?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 59d6c84..7954079 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,9 @@ +const PROMISE_STATUS_ENUM = { + PENDING: "pending", + FULFILLED: "fulfilled", + REJECTED: "rejected" +} + function myPromise(constructor) { let self = this; @@ -8,15 +14,20 @@ function myPromise(constructor) { self.reason = undefined;//定义状态为rejected的时候的状态 function resolve(value) { - // TODO resolve如何改变状态及返回结果 - + if(self.status === PROMISE_STATUS_ENUM.PENDING) { + self.status = PROMISE_STATUS_ENUM.FULFILLED + self.value = value + } } function reject(reason) { // TODO reject如何改变状态及返回结果 - + if(self.status === PROMISE_STATUS_ENUM.PENDING) { + self.status = PROMISE_STATUS_ENUM.REJECTED + self.reason = reason + } } //捕获构造异常 @@ -36,6 +47,11 @@ function myPromise(constructor) { myPromise.prototype.then = function (onFullfilled, onRejected) { //TODO then如何实现 + if(this.status === PROMISE_STATUS_ENUM.FULFILLED) { + onFullfilled(this.value) + } else if (this.status === PROMISE_STATUS_ENUM.REJECTED) { + onRejected(this.reason) + } } module.exports = myPromise From 04ea8ea22aea41561d22f21734159168a1a49865 Mon Sep 17 00:00:00 2001 From: Kappi <454257312@qq.com> Date: Sun, 21 Aug 2022 00:37:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=20Job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/homework8.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/workflows/homework8.yml diff --git a/.github/workflows/homework8.yml b/.github/workflows/homework8.yml new file mode 100644 index 0000000..f4afaf6 --- /dev/null +++ b/.github/workflows/homework8.yml @@ -0,0 +1,29 @@ +# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions + +name: Node.js CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [16.x] + # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ + + steps: + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + - run: yarn + - run: yarn test