一、Promise 类核心逻辑实现
1.Promise 逻辑分析
- Promise 就是一个类 在执行这个类的时候 需要传递一个执行器进去 执行器会立即执行
- Promise 中有三种状态 分别是 成功 fulfilled 、失败 rejected 、 pending
- pending => fulfilled
- pending => rejected
- 一旦状态确定就不可更改
- resolve 和 reject 函数是用来更改状态的
- resolve : fulfilled
- reject : rejected
- then 方法内部就是判断 promise 内部状态,如果是成功,就调用成功回调函数,如果是失败,就调用失败回调函数
2.Promise 逻辑分析代码实现
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
constructor (executor) {
executor(this.resolve, this.reject);
}
status = PENDING;
value = undefined;
reason = undefined;
resolve = value => {
if (this.status != PENDING) return;
this.status = FULFILLED;
this.value = value;
}
reject = reason => {
if (this.status != PENDING) return;
this.status = REJECTED;
this.reason = reason;
}
then(successCallback, failCallback){
if (this.status === FULFILLED) {
successCallback(this.value);
} else if (this.status === REJECTED) {
failCallback(this.reason);
}
}
}
module.exports = MyPromise;
let MyPromise = require('./myPromise');
let promise = new MyPromise ( (resolve, reject) => {
resolve('成功');
reject('失败');
} )
promise.then( value => {
console.log(value);
}, reason => {
console.log(reason);
} )
2.在 Promise 中加入异步逻辑
- 当给 Promise 中的立即执行函数是异步的时候,我们在给 .then 原型方法中就要把传递的两个成功和失败的回调函数给保存在 Promise 这个类的变量中。等待立即执行函数的异步代码调用。
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
status = PENDING;
value = undefined;
reason = undefined;
successCallback = undefined;
failCallback = undefined;
constructor (executor) {
executor(this.resolve, this.reject);
}
resolve = value => {
if (this.status != PENDING) return;
this.status = FULFILLED;
this.value = value;
this.successCallback && this.successCallback(value)
}
reject = reason => {
if (this.status != PENDING) return;
this.status = REJECTED;
this.reason = reason;
this.failCallback && this.failCallback(reason);
}
then(successCallback, failCallback){
if (this.status === FULFILLED) {
successCallback(this.value);
} else if (this.status === REJECTED) {
failCallback(this.reason);
}else {
this.successCallback = successCallback;
this.failCallback = failCallback;
}
}
}
module.exports = MyPromise;
let MyPromise = require('./myPromise');
let promise = new MyPromise ( (resolve, reject) => {
setTimeout(() => {
resolve('成功');
}, 2000);
} )
promise.then( value => {
console.log(value);
}, reason => {
console.log(reason);
} )
3.实现 .then 方法 多次调用添加多个回调函数
- 在 Promise 中多次调用 then 方法时我们应该把 then 方法中传递过来的方法用数组保存起来,然后在执行回调的时候用数组弹出栈的方法取出并调用该方法 shift()
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
status = PENDING;
value = undefined;
reason = undefined;
successCallback = [];
failCallback = []
constructor (executor) {
executor(this.resolve, this.reject);
}
resolve = value => {
if (this.status != PENDING) return;
this.status = FULFILLED;
this.value = value;
while (this.successCallback.length) this.successCallback.shift()(value)
}
reject = reason => {
if (this.status != PENDING) return;
this.status = REJECTED;
this.reason = reason;
while (this.failCallback.length) this.failCallback.shift()(reason);
}
then(successCallback, failCallback){
if (this.status === FULFILLED) {
successCallback(this.value);
} else if (this.status === REJECTED) {
failCallback(this.reason);
}else {
this.successCallback.push(successCallback);
this.failCallback.push(failCallback);
}
}
}
module.exports = MyPromise;
let MyPromise = require('./myPromise');
let promise = new MyPromise ( (resolve, reject) => {
setTimeout(() => {
resolve('成功 .........');
}, 2000);
} )
promise.then( value => {
console.log(1);
console.log(value);
}, reason => {
console.log(reason);
} )
promise.then( value => {
console.log(2);
console.log(value);
}, reason => {
console.log(reason);
} )
promise.then( value => {
console.log(3);
console.log(value);
}, reason => {
console.log(reason);
} )
4. then 方法的链式调用
then 链式调用(上)
promise.then(value => {
console.log(value);
}).then( value => {
console.log(value);
})
- 在链式调用第一个 then 方法的时候 应该返回 一个 Promise 并且立即执行当前 中的方法 , 好让下次继续链式调用
- 返回 Promise 的时候应当记录当前上一个 then 方法返回的参数
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
status = PENDING;
value = undefined;
reason = undefined;
successCallback = [];
failCallback = []
constructor (executor) {
executor(this.resolve, this.reject);
}
resolve = value => {
if (this.status != PENDING) return;
this.status = FULFILLED;
this.value = value;
while (this.successCallback.length) this.successCallback.shift()(value)
}
reject = reason => {
if (this.status != PENDING) return;
this.status = REJECTED;
this.reason = reason;
while (this.failCallback.length) this.failCallback.shift()(reason);
}
then(successCallback, failCallback){
let promise2 = new Promise((resolve, reject) => {
if (this.status === FULFILLED) {
let x= successCallback(this.value);
return resolve(x)
} else if (this.status === REJECTED) {
let y = failCallback(this.reason);
return reject(y)
}else {
this.successCallback.push(successCallback);
this.failCallback.push(failCallback);
}
})
return promise2
}
}
module.exports = MyPromise;
let MyPromise = require('./myPromise');
let promise = new MyPromise ( (resolve, reject) => {
resolve('成功 .........');
} )
promise.then( value => {
console.log(value);
return 100
}).then( value => {
console.log(value);
})
then 链式调用(下)
- 判断额 x 的值是普通对象还是 promise 对象
- 如果是普通值 直接调用 resolve
- 如果是 promise 对象 查看promisee 对象返回的结果
- 再根据 promise 对象返回的结果 决定调用resolve 还是调用reject
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
status = PENDING;
value = undefined;
reason = undefined;
successCallback = [];
failCallback = []
constructor (executor) {
executor(this.resolve, this.reject);
}
resolve = value => {
if (this.status != PENDING) return;
this.status = FULFILLED;
this.value = value;
while (this.successCallback.length) this.successCallback.shift()(value)
}
reject = reason => {
if (this.status != PENDING) return;
this.status = REJECTED;
this.reason = reason;
while (this.failCallback.length) this.failCallback.shift()(reason);
}
then(successCallback, failCallback){
let promise2 = new Promise((resolve, reject) => {
if (this.status === FULFILLED) {
let x= successCallback(this.value);
resolvePromise ( x, resolve, reject);
} else if (this.status === REJECTED) {
let y = failCallback(this.reason);
return reject(y)
}else {
this.successCallback.push(successCallback);
this.failCallback.push(failCallback);
}
})
return promise2
}
}
function resolvePromise ( x, resolve, reject) {
if ( x instanceof MyPromise) {
x.then(resolve, reject)
} else {
resolve(x);
}
}
module.exports = MyPromise;
let MyPromise = require('./myPromise');
let promise = new MyPromise ( (resolve, reject) => {
resolve('成功 .........');
} )
function other() {
return new MyPromise((resolve, reject) => {
resolve('other');
} )
}
promise.then( value => {
console.log(value);
return other()
}).then( value => {
console.log(value);
})
5.then 方法链式调用识别 Promise 对象自返回