Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/homework8.yml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const PROMISE_STATUS_ENUM = {
PENDING: "pending",
FULFILLED: "fulfilled",
REJECTED: "rejected"
}

function myPromise(constructor) {
let self = this;

Expand All @@ -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
}
}

//捕获构造异常
Expand All @@ -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