function MyPromise(fn){
// 保存this指向
that = this;
// 初始化状态
that.status = "pending";
// value 用来保存最后的结果的
that.value = null;
fn(resolve, reject);
resolve = function(value) {
if(that.status === "pending") {
that.statue = "resolve";
that.value = value;
}
}
reject = function(value) {
if(that.status === "pending"){
that.value = value;
}
}
}
MyPromise.prototype.then = function (fn){
if(this.status === "resolve") {
fn(this.value);
}
}
let p = new MyPromise( (resolve, reject) => {
let num = Math.random()*100;
if(num<100){
resolve(num);
}else {
reject(num);
}
} )
p.then( res => console.log(res));