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 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