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完成 #7

Open
wants to merge 1 commit 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
38 changes: 23 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
function myPromise(constructor) {
let self = this;

self.status = "pending" //定义状态改变前的初始状态
self.status = "pending"; //定义状态改变前的初始状态

self.value = undefined;//定义状态为resolved的时候的状态
self.value = undefined; //定义状态为resolved的时候的状态

self.reason = undefined;//定义状态为rejected的时候的状态
self.reason = undefined; //定义状态为rejected的时候的状态

function resolve(value) {

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

if (self.status === "pending") {
self.status = "resolved";
self.value = value;
}
}

function reject(reason) {

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

if (self.status === "pending") {
self.status = "rejected";
self.reason = reason;
}
}

//捕获构造异常

try {

constructor(resolve, reject);

} catch (e) {

reject(e);

}

}

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

//TODO then如何实现
let self = this;
if (self.status === "resolved") {
onFullfilled(self.value);
}
if (self.status === "rejected") {
onRejected(self.reason);
}
};
module.exports = myPromise;

}
module.exports = myPromise
new Promise((resolve, reject) => {
resolve("isOk");
}).then();
Loading