//分析以下Promise实例
// new Promise((res,rej)=>{
// // console.log(‘111’);
// // console.log(res);
// // console.log(rej);
// // res(‘chenggong’);
// throw new Error(‘21’);
// });
/*
1.实例化promise时需要传参数(res,rej),
所以在写源码时候必须构建
2.res和rej都是函数可以执行,调用
3.参数resolve 和 reject 是不是原型上的
或者是否是实例上的
验证:如果上原型或者实例上,我们直接打印
let p = new Promise((resolve,reject)=>{
});
console.log(p.resolve);
此时他会出现值,但是结果是undefind
所以它既不是原型上的,也不是实力上的
4.then(onfulfiled,onrejected)
onfulfiled成功时候调用
onrejected失败时候掉用
成功或者失败的状态需要判断此时的状态
*/
以上肥西并不完全
//promise的三种状态
//等待态
const PENDING = "PENDING";
//成功态
const RESOLVE = "RESOLVE";
//失败态
const REJECT = "REJECT";
function resolvePromise(promise4, x, resolve, reject) {
if (x === promise4) {
return reject(new TypeError('[TypeError: Chaining cycle detected for promise #<Promise>]123'));
}
let called;
if (typeof x === 'object' && x != null || typeof x === 'function') {
try {
let then = x.then;
if (typeof then === 'function') {
then.call(x, y => {
//y是x执行正确的时候调用
if (called) return;
called = true;
resolvePromise(promise4, y, resolve, reject);
}, r => {
//
if