class Promise { constructor(executor) { executor(this.resolve, this.reject); } // 状态 status = "PENDING"; // 返回的结果 value = undefined; //成功的回调 onfulfilled = []; //失败的回调 onrejected = []; resolve = (value) => { if (this.status === "PENDING") { this.status = "FULFILLED"; this.value = value; this.onfulfilled.forEach((item) => item(value)); } }; reject = (value) => { if (this.status === "PENDING") { this.status = "REJECTED"; this.value = value; this.onrejected.forEach((item) => item(value)); } }; then = (onfulfilled, onrejected) => { if (this.status === "FULFILLED") { onfulfilled(this.value); } else if (this.status === "REJECTED") { onrejected(this.value); } else if (this.status === "PENDING") { this.onfulfilled.push(onfulfilled); this.onrejected.push(onrejected); } };}