promise
time 2m41s
class MyPromise {
constructor() {
}
}
/*换成mypromise*/
// const p1 = new Promise((resolve, reject) => {
const p1 = new MyPromise((resolve, reject) => {
resolve(1);
})
p1.then((res) => {
console.log(res);
}, err => {
console.log(err);
})
/*1*/
第一步
time 5m54s
class MyPromise {
constructor(executor) {
this.state = 'pending';
this.value = undefined;
this.reason = undefined;
let resolve = (value) => {
this.state = 'fullFilled';
this.value = value
}
let reject = (reason) => {
this.state = 'rejected';
this.reason = reason;
}
executor(resolve, reject);
}
then(onFullFilled, onRejected) {
if (this.state === 'fullFilled') {
onFullFilled(this.value);
}
if (this.state === 'rejected') {
onRejected(this.reason);
}
}
}
/*换成mypromise*/
// const p1 = new Promise((resolve, reject) => {
const p1 = new MyPromise((resolve, reject) => {
resolve(1);
})
p1.then((res) => {
console.log(res);
}, err => {
console.log(err);
})
/*1*/
处理异步
time 6m35s
class MyPromise {
constructor(executor) {
this.state = 'pending';
this.value = undefined;
this.reason = undefined;
this.onFullFilledCallbacks = [];
this.onRejectedCallbacks = [];
let resolve = (value) => {
this.state = 'fullFilled';
this.value = value;
// this.onFullFilledCallbacks.forEach(fn => fn(this.value));
this.onFullFilledCallbacks.forEach(fn => fn());
}
let reject = (reason) => {
this.state = 'rejected';
this.reason = reason;
// this.onRejectedCallbacks.forEach(fn => fn(this.value));
this.onRejectedCallbacks.forEach(fn => fn());
}
executor(resolve, reject);
}
then(onFullFilled, onRejected) {
if (this.state === 'fullFilled') {
onFullFilled(this.value);
}
if (this.state === 'rejected') {
onRejected(this.reason);
}
if (this.state === 'pending') {
/* this.onFullFilledCallbacks.push(onFullFilled);
this.onRejectedCallbacks.push(onRejected);*/
this.onFullFilledCallbacks.push(() => {
onFullFilled(this.value);
});
this.onRejectedCallbacks.push(()=>{
onRejected(this.reason);
});
}
}
}
/*换成mypromise*/
// const p1 = new Promise((resolve, reject) => {
const p1 = new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve(1);
}, 1000)
})
p1.then((res) => {
console.log(res);
}, err => {
console.log(err);
})
p1.then((res) => {
console.log(res);
}, err => {
console.log(err);
})
/*1 1*/
处理链式操作
time 10m20s
class MyPromise {
constructor(executor) {
this.state = 'pending';
this.value = undefined;
this.reason = undefined;
this.onFullFilledCallbacks = [];
this.onRejectedCallbacks = [];
let resolve = (value) => {
this.state = 'fullFilled';
this.value = value;
// this.onFullFilledCallbacks.forEach(fn => fn(this.value));
this.onFullFilledCallbacks.forEach(fn => fn());
}
let reject = (reason) => {
this.state = 'rejected';
this.reason = reason;
// this.onRejectedCallbacks.forEach(fn => fn(this.value));
this.onRejectedCallbacks.forEach(fn => fn());
}
executor(resolve, reject);
}
then(onFullFilled, onRejected) {
/*因为判断是同步执行的*/
const p2 = new MyPromise((resolve, reject) => {
let x;
if (this.state === 'fullFilled') {
x = onFullFilled(this.value);
// console.log('165:'+x)
resolve(x);
}
if (this.state === 'rejected') {
x = onRejected(this.reason);
resolve(x);
}
if (this.state === 'pending') {
/* this.onFullFilledCallbacks.push(onFullFilled);
this.onRejectedCallbacks.push(onRejected);*/
this.onFullFilledCallbacks.push(() => {
x= onFullFilled(this.value);
resolve(x);
});
this.onRejectedCallbacks.push(() => {
x= onRejected(this.reason);
resolve(x);
});
}
})
return p2;
}
}
/*换成mypromise*/
// const p1 = new Promise((resolve, reject) => {
const p1 = new MyPromise((resolve, reject) => {
// setTimeout(() => {
// resolve(1);
reject(1);
// }, 1000)
})
const p2 = p1.then((res) => {
// console.log(res);
return res + 1;
}, err => {
// console.log(err)
return err + 2;
})
p2.then(res => {
console.log(res,'success');
}, err => {
console.log(err,'error');
})
/*3 success*/
处理new MyPromise
time 16m35s
time 34m06s