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

v1 #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

v1 #13

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
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ function myPromise(constructor) {
function resolve(value) {

// TODO resolve如何改变状态及返回结果
if (this.status === "pending") {
this.status = "fulfilled";
this.value = value;
}

}

function reject(reason) {

// TODO reject如何改变状态及返回结果
if (this.status === "pending") {
this.status = "rejected";
this.reason = reason;
}

}

Expand All @@ -36,6 +44,13 @@ function myPromise(constructor) {
myPromise.prototype.then = function (onFullfilled, onRejected) {

//TODO then如何实现
setTimeout(() => {
if (this.status === "fulfilled") {
onFullfilled(this.value);
} else if (this.status === "rejected") {
onRejected(this.reason);
}
})

}
module.exports = myPromise
module.exports = myPromise
2 changes: 1 addition & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const myPromise = require('./index');

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