Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第二周作业:完成简易promise #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Egg Debug",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"test",
"--",
"--inspect-brk"
],
"console": "integratedTerminal",
"restart": true,
"autoAttachChildProcesses": true
},
]
}
73 changes: 61 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,83 @@ function myPromise(constructor) {
self.reason = undefined;//定义状态为rejected的时候的状态

function resolve(value) {

// TODO resolve如何改变状态及返回结果

self.value = value
if(self.status !== 'pending'){
return
}
self.status = 'resolve'
const { fullfilled } = self
if(fullfilled){
fullfilled(self.value)
}
}

function reject(reason) {

// TODO reject如何改变状态及返回结果
if(self.status !== 'pending'){
return
}
self.reason = reason
self.status = 'reject'
// promise的then函数执行时,this值好像是undefined,所以不能直接this.fullfilled
const { rejected } = self
if(rejected){
rejected(reason)
}

}

//捕获构造异常

try {

constructor(resolve, reject);

} catch (e) {

reject(e);

}

}

myPromise.prototype.then = function (onFullfilled, onRejected) {

//TODO then如何实现

const { status, value, reason} = this
if(!onFullfilled || typeof onFullfilled !== 'function') {
throw new Error('参数异常,最少需要传入完成函数')
}
// 已完成状态直接执行完成函数,并且返回新的已完成状态的promise,数据更新为完成函数的返回值
if(status == 'resolve'){
let result = value
if(onFullfilled && typeof onFullfilled == 'function') {
// 这里可以trycatch,给出合理错误提示
result = onFullfilled(value)
}
return new myPromise(function(resolve){ resolve(result)})
} else if(status == 'reject'){
// 错误状态直接执行错误函数,并且返回新的错误状态的promise,使用同一个错误对象
if(onRejected && typeof onRejected == 'function') {
onRejected(reason)
}
return new myPromise(function(_resolve,reject){ reject(reason)})
} else {
const self = this
// 进行中状态,依然需要返回新的promise对象
// 感觉两个promise对象耦合了,不过目前没有更好的思路
return new myPromise(function(resolve,reject){
self.fullfilled = function(value){
let result = value
// 看别人的作业,想起来这里报错了下个promise要进入reject状态
try {
result = onFullfilled(value)
resolve(result)
} catch (error) {
reject(error)
}
}
self.rejected = function(reason){
if(onRejected && typeof onRejected == 'function') {
onRejected(reason)
}
reject(reason)
}
})
}
}

module.exports = myPromise
100 changes: 97 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,103 @@
const myPromise = require('./index');

var p = new myPromise(function (resolve, reject) { resolve('isOk') });
const p1 = new myPromise(function (resolve, reject) { resolve('isOk') });
test('测试primose的then是否成功', () => {
expect.assertions(1);
return p.then(data => {
p1.then(data => {
expect(data).toBe('isOk');
});
});
});


test('测试异步primose的then是否成功', async () => {
expect.assertions(1);
const p2 = new myPromise(function (resolve, reject) {
setTimeout(()=>
resolve('isOk')
, 1000)
});
await p2.then(data => {
expect(data).toBe('isOk');
});
});


test('测试primose的then函数的错误函数入参', () => {
expect.assertions(1);
const p3 = new myPromise(function (_resolve, reject) {
reject('isno')
});
p3.then(data => {
expect(data).toBe('isOk');
}, err => {
expect(err).toBe('isno');
});
});

const p4 = new myPromise(function (_resolve, reject) {
setTimeout(()=> reject('isno'), 1000)
});
test('测试异步primose的then函数的错误函数入参', () => {
expect.assertions(1);
p4.then(data => {
expect(data).toBe('isOk');
}, err => {
expect(err).toBe('isno');
});
});

const p5 = new myPromise(function (resolve, reject) { resolve('isOk') });
test('测试primose的连续then是否成功', () => {
expect.assertions(2);
p5.then(data => {
expect(data).toBe('isOk');
return 'isOk'
}).then(data => {
expect(data).toBe('isOk');
});
});

const p6 = new myPromise(function (resolve, reject) {
setTimeout(()=> resolve('isOk'), 1000)
});
test('测试异步primose的连续then是否成功', () => {
expect.assertions(2);
p6.then(data => {
expect(data).toBe('isOk');
return 'isOk'
}).then(data => {
expect(data).toBe('isOk');
});
});

const p7 = new myPromise(function (_resolve, reject) {
reject('isno')
});
test('测试primose的连续then函数的错误函数入参', () => {
expect.assertions(2);
p7.then(data => {
expect(data).toBe('isOk');
},err => {
expect(err).toBe('isno');
}).then(data => {
expect(data).toBe('isOk');
},err => {
expect(err).toBe('isno');
});
});

const p8 = new myPromise(function (_resolve, reject) {
setTimeout(()=> reject('isno'), 1000)
});
test('测试异步primose的连续then函数的错误函数入参', () => {
expect.assertions(2);
p8.then(data => {
expect(data).toBe('isOk');
},err => {
expect(err).toBe('isno');
}).then(data => {
expect(data).toBe('isOk');
},err => {
expect(err).toBe('isno');
});
});
40 changes: 40 additions & 0 deletions yarn-error.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Arguments:
D:\Program Files\nodejs\node.exe C:\Users\cxs\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js install

PATH:
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;D:\Git\cmd;D:\Python27;D:\Program Files\nodejs;C:\Users\cxs\AppData\Local\Microsoft\WindowsApps;;D:\VSCode\bin;C:\Users\cxs\AppData\Local\Pandoc\;C:\Users\cxs\AppData\Roaming\npm

Yarn version:
1.22.18

Node version:
16.15.0

Platform:
win32 x64

Trace:
Error: connect ETIMEDOUT 192.168.252.160:4873
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)

npm manifest:
{
"name": "homework8",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "ccpro",
"license": "MIT",
"devDependencies": {
"jest": "^28.1.3"
}
}

yarn manifest:
No manifest

Lockfile:
No lockfile
Loading