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

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
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ function myPromise(constructor) {
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);
Expand All @@ -35,7 +38,14 @@ function myPromise(constructor) {

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

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

}
module.exports = myPromise
Loading