如何使用promise
promise 的基础概念
Promise 对象标识一个异步操作,有三种状态,pending fulfilled rejected,当 promise 被调用之后它会显示 pending 状态,被 创建的 promise 最后会以 fulfilled 或者 rejected 状态结束
来看这样一段代码
let myPromise2 = new Promise((resolve, reject) => {
let a = 1;
for (let index = 0; index < 5; index++) {
a++;
}
});
console.log("myPromise2 :>> ", myPromise2);
myPromise2.then(() => {
console.log("myPromise2执行了then");
});
在 pending 状态下,promise 不会执行回调函数 then
手写 promise
通过手写 promise
来了解 promise
的实现原理
回忆一下一刚刚整理的 promise
,promise
是有两个参数的,一个是 resolve
表示成功,一个是 reject
表示拒绝,由此还可以引申推广到,promise
的三个状态,分别是 pending
, fulfilled
, rejected
promise 肯定是一个类,这个类有 resolve 和 reject 两个方法,还有一个 then 方法,then 方法接收两个参数,一个是成功的回调,一个是失败的回调,then 方法还会返回一个新的 promise
class myPromise {
constructor(executor) {
let resolve = () => {}; //成功
let reject = () => {}; //失败
executor(resolve, reject);
}
}
然后根据三个状态
class myPromise {
constructor(executor) {
this.state = "pending";
this.value = undefined;
this.reason = undefined;
let resolve = (value) => {
if (this.state === "pending") {
this.state = "fulfilled";
this.value = value;
}
};
let reject = (reason) => {
if (this.state === "pending") {
this.state = "rejected";
this.reason = reason;
}
};
try {
executor(resolve, reject);
} catch (e) {
reject(e);
}
}
}
平时我们使用完了这些东西之后会使用.then
方法来做下一步操作
class myPromise {
constructor(executor) {
this.state = "pending";
this.value = undefined;
this.reason = undefined;
this.onResolvedCallbacks = [];
this.onRejectedCallbacks = [];
let resolve = (value) => {
if (this.state === "pending") {
this.state = "fulfilled";
this.value = value;
this.onResolvedCallbacks.forEach((fn) => fn());
}
};
let reject = (reason) => {
if (this.state === "pending") {
this.state = "rejected";
this.reason = reason;
this.onRejectedCallbacks.forEach((fn) => fn());
}
};
try {
executor(resolve, reject);
} catch (e) {
reject(e);
}
}
then(onFulfilled, onRejected) {
if (this.state === "fulfilled") {
onFulfilled(this.value);
}
if (this.state === "rejected") {
onRejected(this.reason);
}
if (this.state === "pending") {
this.onResolvedCallbacks.push(() => {
onFulfilled(this.value);
});
this.onRejectedCallbacks.push(() => {
onRejected(this.reason);
});
}
}
}
如果 then 中如果 onFulfilled 和 onRejected 不是函数,那么我们就忽略它,让值穿透
class myPromise {
constructor(executor) {
this.state = "pending";
this.value = undefined;
this.reason = undefined;
// executor(this.resolve.bind(this), this.reject.bind(this));
try {
executor(this.resolve.bind(this), this.reject.bind(this));
} catch (e) {
this.reject(e);
}
}
resolve(value) {
if (this.state === "pending") {
this.state = "fulfilled";
this.value = value;
}
}
reject(reason) {
if (this.state === "pending") {
this.state = "rejected";
this.reason = reason;
}
}
then(onFulfilled, onRejected) {
onFulfilled =
typeof onFulfilled === "function" ? onFulfilled : (value) => value;
onRejected =
typeof onRejected === "function"
? onRejected
: (reason) => {
throw reason;
};
if (this.state === "fulfilled") {
onFulfilled(this.value);
}
if (this.state === "rejected") {
onRejected(this.reason);
}
}
}