代碼塊中的省略號(hào),代表相較于上次代碼未改動(dòng)部分
1)核心邏輯實(shí)現(xiàn)
分析:
- 根據(jù)調(diào)用方式可知,promise是一個(gè)類(lèi),需要傳遞一個(gè)執(zhí)行器進(jìn)去,執(zhí)行器會(huì)立即執(zhí)行
- promise有三種狀態(tài),分別為成功-fulfilled 失敗-rejected 等待-pending,一旦狀態(tài)確定就不可改變
pending > fulfilled
pending > rejected - resolve和reject函數(shù)是用來(lái)改變狀態(tài)的,resolve是成功,reject是失??;
- then接受兩個(gè)參數(shù),如果狀態(tài)成功就調(diào)用成功回調(diào)函數(shù)(參數(shù)代表成功結(jié)果),否則就調(diào)用失敗回調(diào)(參數(shù)代表失敗原因)
分析完畢,開(kāi)搞:
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
constructor(exector) {
// exector是一個(gè)執(zhí)行器,傳入resolve和reject方法,進(jìn)入會(huì)立即執(zhí)行,
exector(this.resolve, this.reject);
}
// 實(shí)例對(duì)象上屬性,初始狀態(tài)為等待
status = PENDING;
// 成功后的值
value = undefined;
// 失敗后的原因
reason = undefined;
// 使用箭頭函數(shù),讓this指向當(dāng)前實(shí)例對(duì)象
resolve = (value) => {
// 判斷狀態(tài)不是等待,阻止執(zhí)行
if (this.status !== PENDING) return;
// 將狀態(tài)改為成功,并保存成功值
this.status = FULFILLED;
this.value = value;
};
reject = (reason) => {
if (this.status !== PENDING) return;
// 將狀態(tài)改為失敗,并保存失敗原因
this.status = REJECTED;
this.reason = reason;
};
then(successCallback, failCallback) {
if (this.status === FULFILLED) {
// 調(diào)用成功回調(diào),把結(jié)果返回
successCallback(this.value);
} else if (this.status === REJECTED) {
// 調(diào)用失敗回調(diào),把錯(cuò)誤信息返回
failCallback(this.reason);
}
}
}
2)加入異步處理邏輯
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
constructor(exector) {
// exector是一個(gè)執(zhí)行器,傳入resolve和reject方法,進(jìn)入會(huì)立即執(zhí)行,
exector(this.resolve, this.reject);
}
// 實(shí)例對(duì)象上屬性,初始狀態(tài)為等待
status = PENDING;
// 成功后的值
value = undefined;
// 失敗后的原因
reason = undefined;
// 定義成功回調(diào)和失敗回調(diào)參數(shù)
successCallback = undefined;
failCallback = undefined;
// 使用箭頭函數(shù),讓this指向當(dāng)前實(shí)例對(duì)象
resolve = (value) => {
// 判斷狀態(tài)不是等待,阻止執(zhí)行
if (this.status !== PENDING) return;
// 將狀態(tài)改為成功,并保存成功值
this.status = FULFILLED;
this.value = value;
this.successCallback && this.successCallback(this.value);
};
reject = (reason) => {
if (this.status !== PENDING) return;
// 將狀態(tài)改為失敗,并保存失敗原因
this.status = REJECTED;
this.reason = reason;
this.failCallback && this.failCallback(this.reason);
};
then(successCallback, failCallback) {
if (this.status === FULFILLED) {
// 調(diào)用成功回調(diào),把結(jié)果返回
successCallback(this.value);
} else if (this.status === REJECTED) {
// 調(diào)用失敗回調(diào),把錯(cuò)誤信息返回
failCallback(this.reason);
} else {
// 等待狀態(tài),把成功和失敗回調(diào)暫存起來(lái)
this.successCallback = successCallback;
this.failCallback = failCallback;
}
}
}
3)then 方法多次調(diào)用
- promise的then是可以被多次調(diào)用的,
- 如下例子,如果三個(gè)then調(diào)用,都是同步調(diào)用,則直接返回值即可;
- 如果是異步調(diào)用,那么成功回調(diào)和失敗回調(diào)應(yīng)該是多個(gè)不同的;
let promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('success')
}, 2000);
})
promise.then(value => {
console.log(1)
console.log('resolve', value) //resolve success
})
promise.then(value => {
console.log(2)
console.log('resolve', value) //resolve success
})
promise.then(value => {
console.log(3)
console.log('resolve', value) //resolve success
})
所以需要改進(jìn):把回調(diào)放進(jìn)數(shù)組,待狀態(tài)確定后統(tǒng)一執(zhí)行
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
constructor(exector) {
// exector是一個(gè)執(zhí)行器,傳入resolve和reject方法,進(jìn)入會(huì)立即執(zhí)行,
exector(this.resolve, this.reject);
}
// 實(shí)例對(duì)象上屬性,初始狀態(tài)為等待
status = PENDING;
// 成功后的值
value = undefined;
// 失敗后的原因
reason = undefined;
// 定義成功回調(diào)和失敗回調(diào)參數(shù),初始化空數(shù)組
successCallback = [];
failCallback = [];
// 使用箭頭函數(shù),讓this指向當(dāng)前實(shí)例對(duì)象
resolve = (value) => {
// 判斷狀態(tài)不是等待,阻止執(zhí)行
if (this.status !== PENDING) return;
// 將狀態(tài)改為成功,并保存成功值
this.status = FULFILLED;
this.value = value;
while (this.successCallback.length) {
this.successCallback.shift()(this.value);
}
};
reject = (reason) => {
if (this.status !== PENDING) return;
// 將狀態(tài)改為失敗,并保存失敗原因
this.status = REJECTED;
this.reason = reason;
while (this.failCallback.length) {
this.failCallback.shift()(this.reason);
}
};
then(successCallback, failCallback) {
if (this.status === FULFILLED) {
// 調(diào)用成功回調(diào),把結(jié)果返回
successCallback(this.value);
} else if (this.status === REJECTED) {
// 調(diào)用失敗回調(diào),把錯(cuò)誤信息返回
failCallback(this.reason);
} else {
// 等待狀態(tài),把成功和失敗回調(diào)暫存到數(shù)組中
this.successCallback.push(successCallback);
this.failCallback.push(failCallback);
}
}
}
4)then 方法鏈?zhǔn)秸{(diào)用
- then方法會(huì)返回一個(gè)新的Promise實(shí)例。因此可以采用鏈?zhǔn)綄?xiě)法
- then方法可以返回一個(gè)普通值或者一個(gè)新的promise實(shí)例
返回普通值用法:
let promise = new Promise((resolve, reject) => {
// 目前這里只處理同步的問(wèn)題
resolve('success');
});
promise
.then((value) => {
console.log('1', value); // 1 success
return 'hello';
})
.then((value) => {
console.log('2', value); // 2 hello
});
返回新的promise實(shí)例用法:
let promise = new Promise((resolve, reject) => {
// 目前這里只處理同步的問(wèn)題
resolve('success');
});
function other() {
return new Promise((resolve, reject) => {
resolve('other');
});
}
promise
.then((value) => {
console.log('1', value); // 1 success
return other();
})
.then((value) => {
console.log('2', value); // 2 other
});
實(shí)現(xiàn):
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
constructor(exector) {
exector(this.resolve, this.reject);
}
status = PENDING;
value = undefined;
reason = undefined;
successCallback = [];
failCallback = [];
resolve = (value) => {
if (this.status !== PENDING) return;
this.status = FULFILLED;
this.value = value;
while (this.successCallback.length)
this.successCallback.shift()(this.value);
};
reject = (reason) => {
if (this.status !== PENDING) return;
this.status = REJECTED;
this.reason = reason;
while (this.failCallback.length) this.failCallback.shift()(this.reason);
};
then(successCallback, failCallback) {
// then方法返回第一個(gè)promise對(duì)象
let promise2 = new MyPromise((resolve, reject) => {
if (this.status === FULFILLED) {
// x是上一個(gè)promise回調(diào)函數(shù)的返回結(jié)果
// 判斷x是普通值還是promise實(shí)例
// 如果是普通值,直接resolve
// 如果是promise實(shí)例,待promise狀態(tài)變?yōu)閒ulfilled,調(diào)用resolve或者reject
// 因?yàn)閙ew MyPromise需要執(zhí)行完才能拿到promise2,所以通過(guò)異步拿到
setTimeout(() => {
let x = successCallback(this.value);
resolvePromise(promise2, x, resolve, reject);
}, 0);
} else if (this.status === REJECTED) {
failCallback(this.reason);
} else {
this.successCallback.push(successCallback);
this.failCallback.push(failCallback);
}
});
return promise2;
}
}
function resolvePromise(promise2, x, resolve, reject) {
// 如果等于了,說(shuō)明返回了自身,報(bào)錯(cuò)
if (promise2 === x) {
return reject(
new TypeError('Chaining cycle detected for promise #<Promise>')
);
}
// 判斷x是不是其實(shí)例對(duì)象
if (x instanceof MyPromise) {
x.then(
(value) => resolve(value),
(reason) => reject(reason)
);
} else {
// 普通值
resolve(x);
}
}
5)捕獲錯(cuò)誤及優(yōu)化
- 捕獲執(zhí)行器中的錯(cuò)誤
constructor(exector) {
// 捕獲錯(cuò)誤,如果有錯(cuò)誤就執(zhí)行reject
try {
exector(this.resolve, this.reject);
} catch (e) {
this.reject(e);
}
}
驗(yàn)證:
let promise = new MyPromise((resolve, reject) => {
// resolve('success')
throw new Error('執(zhí)行器錯(cuò)誤')
})
promise.then(value => {
console.log(1)
console.log('resolve', value)
}, reason => {
console.log(2)
console.log(reason.message)
})
//2
//執(zhí)行器錯(cuò)誤
- then執(zhí)行的時(shí)候報(bào)錯(cuò)捕獲
then(successCallback, failCallback) {
// then方法返回第一個(gè)promise對(duì)象
let promise2 = new MyPromise((resolve, reject) => {
if (this.status === FULFILLED) {
setTimeout(() => {
try {
let x = successCallback(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
} else if (this.status === REJECTED) {
failCallback(this.reason);
} else {
this.successCallback.push(successCallback);
this.failCallback.push(failCallback);
}
});
return promise2;
}
驗(yàn)證:
let promise = new MyPromise((resolve, reject) => {
resolve('success');
});
// 第一個(gè)then方法中的錯(cuò)誤要在第二個(gè)then方法中捕獲到
promise
.then(
(value) => {
console.log('1', value);
throw new Error('then error');
},
(reason) => {
console.log('2', reason.message);
}
)
.then(
(value) => {
console.log('3', value);
},
(reason) => {
console.log('4', reason.message);
}
);
// 1 success
// 4 then error
- 錯(cuò)誤后的鏈?zhǔn)秸{(diào)用
then(successCallback, failCallback) {
// then方法返回第一個(gè)promise對(duì)象
let promise2 = new MyPromise((resolve, reject) => {
if (this.status === FULFILLED) {
setTimeout(() => {
try {
let x = successCallback(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
} else if (this.status === REJECTED) {
setTimeout(() => {
try {
let x = failCallback(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
} else {
this.successCallback.push(successCallback);
this.failCallback.push(failCallback);
}
});
return promise2;
}
驗(yàn)證:
let promise = new MyPromise((resolve, reject) => {
throw new Error('執(zhí)行器錯(cuò)誤');
});
// 第一個(gè)then方法中的錯(cuò)誤要在第二個(gè)then方法中捕獲到
promise
.then(
(value) => {
console.log('1', value);
throw new Error('then error');
},
(reason) => {
console.log('2', reason.message);
return 200;
}
)
.then(
(value) => {
console.log('3', value);
},
(reason) => {
console.log('4', reason.message);
}
);
// 2 執(zhí)行器錯(cuò)誤
// 3 200
- 異步狀態(tài)下鏈?zhǔn)秸{(diào)用(then方法優(yōu)化)
...
resolve = (value) => {
...
// 調(diào)用時(shí),不再需要傳值,因?yàn)樵趐ush回調(diào)數(shù)組時(shí),已經(jīng)處理了
while (this.successCallback.length) this.successCallback.shift()();
};
reject = (reason) => {
...
// 調(diào)用時(shí),不再需要傳值,因?yàn)樵趐ush回調(diào)數(shù)組時(shí),已經(jīng)處理了
while (this.failCallback.length) this.failCallback.shift()();
};
then(successCallback, failCallback) {
// then方法返回第一個(gè)promise對(duì)象
let promise2 = new MyPromise((resolve, reject) => {
if (this.status === FULFILLED) {
...
} else if (this.status === REJECTED) {
...
} else {
// 處理異步情況
this.successCallback.push(() => {
setTimeout(() => {
try {
let x = successCallback(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
});
this.failCallback.push(() => {
setTimeout(() => {
// 如果回調(diào)中報(bào)錯(cuò)的話就執(zhí)行reject
try {
let x = failCallback(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
});
}
});
return promise2;
}
驗(yàn)證:
let promise = new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve('成功');
}, 2000);
});
// 第一個(gè)then方法中的錯(cuò)誤要在第二個(gè)then方法中捕獲到
promise
.then(
(value) => {
console.log('1', value);
return 'hello';
},
(reason) => {
console.log('2', reason.message);
return 200;
}
)
.then(
(value) => {
console.log('3', value);
},
(reason) => {
console.log('4', reason.message);
}
);
// 1 成功
// 3 hello
6)把then方法的參數(shù)變成可選參數(shù)
var promise = new Promise((resolve, reject) => {
resolve(100)
})
promise
.then()
.then()
.then()
.then(value => console.log(value))
// 在控制臺(tái)最后一個(gè)then中輸出了100
// 這個(gè)相當(dāng)于
promise
.then(value => value)
.then(value => value)
.then(value => value)
.then(value => console.log(value))
所以修改then方法:
then(successCallback, failCallback) {
// 先判斷回調(diào)函數(shù)是否傳了,如果沒(méi)預(yù)留就默認(rèn)一個(gè)函數(shù),把參數(shù)返回
successCallback = successCallback ? successCallback : (value) => value;
failCallback = failCallback ? failCallback : (reason) => { throw reason };
...
}
7)實(shí)現(xiàn)Promise.all
promise.all方法是解決異步并發(fā)問(wèn)題的
// 如果p1是兩秒之后執(zhí)行的,p2是立即執(zhí)行的,那么根據(jù)正常的是p2在p1的前面。
// 如果我們?cè)赼ll中指定了執(zhí)行順序,那么會(huì)根據(jù)我們傳遞的順序進(jìn)行執(zhí)行。
function p1 () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('p1')
}, 2000)
})
}
function p2 () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('p2')
},0)
})
}
Promise.all(['a', 'b', p1(), p2(), 'c']).then(result => {
console.log(result)
// ["a", "b", "p1", "p2", "c"]
})
分析:
- all方法接收一個(gè)數(shù)組,數(shù)組中可以是普通值也可以是promise對(duì)象
- 數(shù)組中值得順序一定是我們得到的結(jié)果的順序
- 返回值也是一個(gè)promise對(duì)象,可以調(diào)用then方法
- 如果數(shù)組中所有值是成功的,那么then里面就是成功回調(diào),如果有一個(gè)值是失敗的,那么then里面就是失敗的
- 使用all方法是用類(lèi)直接調(diào)用,那么all一定是一個(gè)靜態(tài)方法
分析完,開(kāi)整:
class MyPromise {
...
static all(array) {
// 結(jié)果數(shù)組
let result = [];
// 計(jì)數(shù)器
let index = 0;
return new MyPromise((resolve, reject) => {
let addData = (key, value) => {
result[key] = value;
index++;
// 當(dāng)計(jì)數(shù)器等于參數(shù)數(shù)組的長(zhǎng)度,說(shuō)明所有參數(shù)已經(jīng)執(zhí)行完畢
if (index === array.length) {
resolve(result);
}
};
// 對(duì)傳遞的數(shù)組中遍歷
for (let i = 0; i < array.length; i++) {
let current = array[i];
if (current instanceof MyPromise) {
current.then(
(value) => addData(i, value),
(reason) => reject(reason)
);
} else {
addData(i, array[i]);
}
}
});
}
}
驗(yàn)證:
function p1() {
return new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve('p1');
}, 2000);
});
}
function p2() {
return new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve('p2');
}, 0);
});
}
Promise.all(['a', 'b', p1(), p2(), 'c']).then((result) => {
console.log(result);
// ["a", "b", "p1", "p2", "c"]
});
8)實(shí)現(xiàn)Promise.resolve
分析:
- 如果參數(shù)是一個(gè)promise對(duì)象,則直接返回;如果是一個(gè)值,則生成一個(gè)promise對(duì)象,把值進(jìn)行返回
- 肯定是一個(gè)靜態(tài)方法
class MyPromise {
...
// Promise.resolve方法
static resolve(value) {
if (value instanceof MyPromise) {
return value;
} else {
return new MyPromise((resolve) => resolve(value));
}
}
}
驗(yàn)證:
function p1() {
return new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve('p1');
}, 2000);
});
}
Promise.resolve(100).then((value) => console.log(value));
Promise.resolve(p1()).then((value) => console.log(value));
// 100
// 2s 之后輸出 p1
8)實(shí)現(xiàn)finally方法
- 無(wú)論最終狀態(tài)是成功或是失敗,finally都會(huì)執(zhí)行
- 可以在finally之后拿到then的結(jié)果
- 這是原型上的方法
// finally
// 使用then方法拿到promise的狀態(tài),無(wú)論成功或失敗都返回callback
// then方法返回的就是一個(gè)promise,拿到成功回調(diào)就把value return,錯(cuò)誤回調(diào)就把錯(cuò)誤信息return
// 如果callback是一個(gè)異步promise,還需等待其執(zhí)行完畢,所以要用到靜態(tài)方法resolve
finally(callback) {
return this.then(
(value) => {
return MyPromise.resolve(callback()).then(() => value);
},
(reason) => {
return MyPromise.resolve(callback()).then(() => {
throw reason;
});
}
);
}
驗(yàn)證:
function p1() {
return new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve('p1');
}, 2000);
});
}
function p2() {
return new MyPromise((resolve, reject) => {
reject('p2 reject');
});
}
p2()
.finally(() => {
console.log('finallyp2');
return p1();
})
.then(
(value) => {
console.log('成功回調(diào)', value);
},
(reason) => {
console.log('失敗回調(diào)', reason);
}
);
// finallyp2
// 兩秒之后執(zhí)行p2 reject
9)實(shí)現(xiàn)catch方法
- 可以捕獲全局錯(cuò)誤
- 也是原型對(duì)象的方法
// 直接調(diào)用then方法,然后成功的地方傳遞undefined,錯(cuò)誤的地方傳遞reason
catch(failCallback) {
return this.then(undefined, failCallback);
}
Promise全部代碼
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
constructor(exector) {
// 捕獲錯(cuò)誤,如果有錯(cuò)誤就執(zhí)行reject
try {
exector(this.resolve, this.reject);
} catch (e) {
this.reject(e);
}
}
status = PENDING;
value = undefined;
reason = undefined;
successCallback = [];
failCallback = [];
resolve = (value) => {
if (this.status !== PENDING) return;
this.status = FULFILLED;
this.value = value;
// 調(diào)用時(shí),不再需要傳值,因?yàn)樵趐ush回調(diào)數(shù)組時(shí),已經(jīng)處理了
while (this.successCallback.length) this.successCallback.shift()();
};
reject = (reason) => {
if (this.status !== PENDING) return;
this.status = REJECTED;
this.reason = reason;
// 調(diào)用時(shí),不再需要傳值,因?yàn)樵趐ush回調(diào)數(shù)組時(shí),已經(jīng)處理了
while (this.failCallback.length) this.failCallback.shift()();
};
then(successCallback, failCallback) {
// 先判斷回調(diào)函數(shù)是否傳了,如果沒(méi)預(yù)留就默認(rèn)一個(gè)函數(shù),把參數(shù)返回
successCallback = successCallback ? successCallback : (value) => value;
failCallback = failCallback
? failCallback
: (reason) => {
throw reason;
};
// then方法返回第一個(gè)promise對(duì)象
let promise2 = new MyPromise((resolve, reject) => {
if (this.status === FULFILLED) {
// x是上一個(gè)promise回調(diào)函數(shù)的返回結(jié)果
// 判斷x是普通值還是promise實(shí)例
// 如果是普通值,直接resolve
// 如果是promise實(shí)例,待promise狀態(tài)變?yōu)閒ulfilled,調(diào)用resolve或者reject
// 因?yàn)閙ew MyPromise需要執(zhí)行完才能拿到promise2,所以通過(guò)異步拿到
setTimeout(() => {
try {
let x = successCallback(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
} else if (this.status === REJECTED) {
setTimeout(() => {
try {
let x = failCallback(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
} else {
// 處理異步情況
this.successCallback.push(() => {
setTimeout(() => {
try {
let x = successCallback(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
});
this.failCallback.push(() => {
setTimeout(() => {
// 如果回調(diào)中報(bào)錯(cuò)的話就執(zhí)行reject
try {
let x = failCallback(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
});
}
});
return promise2;
}
static all(array) {
// 結(jié)果數(shù)組
let result = [];
// 計(jì)數(shù)器
let index = 0;
return new MyPromise((resolve, reject) => {
let addData = (key, value) => {
result[key] = value;
index++;
// 當(dāng)計(jì)數(shù)器等于參數(shù)數(shù)組的長(zhǎng)度,說(shuō)明所有參數(shù)已經(jīng)執(zhí)行完畢
if (index === array.length) {
resolve(result);
}
};
// 對(duì)傳遞的數(shù)組中遍歷
for (let i = 0; i < array.length; i++) {
let current = array[i];
if (current instanceof MyPromise) {
current.then(
(value) => addData(i, value),
(reason) => reject(reason)
);
} else {
addData(i, array[i]);
}
}
});
}
// Promise.resolve方法
static resolve(value) {
if (value instanceof MyPromise) {
return value;
} else {
return new MyPromise((resolve) => resolve(value));
}
}
// finally
// 使用then方法拿到promise的狀態(tài),無(wú)論成功或失敗都返回callback
// then方法返回的就是一個(gè)promise,拿到成功回調(diào)就把value return,錯(cuò)誤回調(diào)就把錯(cuò)誤信息return
// 如果callback是一個(gè)異步promise,還需等待其執(zhí)行完畢,所以要用到靜態(tài)方法resolve
finally(callback) {
return this.then(
(value) => {
return MyPromise.resolve(callback()).then(() => value);
},
(reason) => {
return MyPromise.resolve(callback()).then(() => {
throw reason;
});
}
);
}
// catch
// 直接調(diào)用then方法,然后成功的地方傳遞undefined,錯(cuò)誤的地方傳遞reason
catch(failCallback) {
return this.then(undefined, failCallback);
}
}
function resolvePromise(promise2, x, resolve, reject) {
// 如果等于了,說(shuō)明返回了自身,報(bào)錯(cuò)
if (promise2 === x) {
return reject(
new TypeError('Chaining cycle detected for promise #<Promise>')
);
}
// 判斷x是不是其實(shí)例對(duì)象
if (x instanceof MyPromise) {
x.then(
(value) => resolve(value),
(reason) => reject(reason)
);
} else {
// 普通值
resolve(x);
}
}