共计 4880 个字符,预计需要花费 13 分钟才能阅读完成。
Promise
三种状态
- pending 初始状态
- fulfilled 胜利状态
- rejected 回绝状态
初始状态
let p = new Promise(() => {})
console.log(p);
胜利状态
let p = new Promise((resolve) => {resolve()
})
console.log(p);
回绝状态
let p = new Promise((resolve, reject) => {reject()
})
console.log(p);
繁多状态与状态直达
繁多状态:Promise 的状态变为 fulfilled 或 resolved 不会再扭转
let p = new Promise((resolve, reject) => {resolve()
reject()})
console.log(p);
状态直达:resolve()接管一个 Promise 时会依照接管的状态传递
let rej = new Promise((resolve, reject) => {reject()
})
let p = new Promise((resolve, reject) => {resolve(rej)
})
console.log(rej);
console.log(p);
办法
- then
- catch
- finally
- done
- resolve
- reject
- all
- allSettled
- race
- any
- try
1.then(callback1,callback2)
接管 resolve 或 reject 的返回值,依据不同状态调用回调函数
then 返回的也是Promise
,解决后 fulfilled,报错为 rejected
let p = new Promise((resolve, reject) => {reject("回绝")
})
let p1 = p.then(null, res => {console.log(res);
})
console.log(p1);
- then 返回一个 Promise 能够持续解决
new Promise((resolve, reject) => {reject("回绝")
}).then(null, err => {console.log(err);
return err
}).then(err => {console.log(err);
})
// 回绝
// 回绝
2.catch
等价于 .then(null,rejection)
用于指定产生 谬误
时的回调函数
new Promise((resolve, reject) => {
try {throw new Error('谬误');
}
catch (e) {reject(e)
}// 等价于 reject(new Error("谬误"))
}).then(val => {console.log(val);
}).catch(res => {console.log(res);
})
- catch 捕捉 try 中抛出的谬误,而后由 reject 扭转状态并传递
- 谬误具备 ” 冒泡性质 ”,会向下传递,直到被 catch 捕捉
- 反对 then 解决 resolve,catch 解决 reject,catch 能够去捕捉 then 中的谬误,相似于 try/catch 的写法
- resolve 后再抛出谬误不会被捕捉,也不会传递到外层代码
new Promise((resolve, reject) => {resolve("承受")
throw new Error('谬误');
}).then(val => {console.log(val); // 承受
}).catch(res => {console.log(res);
})
3.finally
无论 Promise 对象最初状态是 fulfilled 还是 rejected,都会执行其中的回调函数
finally 的实现:
Promise.prototype.finally = function (callback) {
let P = this.constructor
return this.then(value => P.resolve(callback()).then(()=>value),
reason => P.resolve(callback()).then(()=>{throw reason})
)
}
new Promise((resolve, reject) => {reject("谬误")
}).finally(() => {console.log(1); //1
}).catch(res => {console.log(res);// 谬误
})
- 能够看出 finally 执行一个一般的回调函数,状态会向下传递
4.done
最初的谬误无奈捕捉,能够用 done 抛出一个全局谬误
done 的实现
Promise.prototype.done = function (onFulfilled,onRejected) {this.then(onFulfilled,onRejected)
.catch(function(reason){setTimeout(()=>{throw reason},0)
})
}
new Promise((resolve, reject) => {reject(0)
}).done(val => {console.log(val);
}, res => {console.log(res);
})
- done 也能够像 then 一样应用
5.Promise.resolve() 与 Promise.reject()
将一个对象转为 Promise 对象
以 Promise.resolve(参数)为例
Promise.resolve("foo")
// 等价于
new Promise(resolve => resolve("foo"))
- 参数为 Promise 的实例
-
Promise.resolve 将不做任何批改
let p = new Promise((resolve, reject) => {reject("谬误") }) let p1 = Promise.resolve(p) console.log(p1);
- 参数是一个 thenable 对象,有 then 办法的对象
- Promise.resolve()会将这个对象转成 Promise 对象
-
而后立刻执行 thenable 中的 then 办法
let thenable = {then: function (resolve, reject) {resolve("承受") } } Promise.resolve(thenable) .then(val => {console.log(val);// 承受 })
- 参数没有 then 办法或不是对象
-
Promise.resolve()返回一个新的 Promise 对象,状态为 fulfilled
let obj = {a: 0} Promise.resolve(obj) .then(val => {console.log(val.a);//a })
- 不带任何参数
-
间接返回一个 fulfilled 状态的 Promise 对象
Promise.resolve() .then(() => {console.log("承受状态");// 承受转态 })
Promise.reject()也会返回一个新的 Promise 实例,状态为 rejected
与 Promise.resolve()不同的是,Promise.reject()的参数会一成不变的传递
6.Promise.all()与 Promise.allSettled()
- Promise.all 能够将
多个
Promise 实例包装成一个
新的 Promise 实例 - 如果不是则会调用 Promise.resolve 办法,转为 Promise 实例
- 参数为 iterable 类型
-
Promise.all 返回的 Promise 的
状态
取决于参数的状态
,全副 fulfilled 为 fulfilled,有一个 rejected
返回第一个 rejectedlet p1 = Promise.resolve("胜利") let p2 = "foo" let p3 = Promise.reject("失败 1") let p4 = Promise.reject("失败 2") .catch(res => {console.log("本人的" + res);// 本人的失败 2 return res }) Promise.all([p1, p2, p4]) .then(val => {console.log(val);//["胜利","foo","失败 2"] }) Promise.all([p2, p4, p3]) .then(val => {console.log(val); }) .catch(res => {console.log("all 的" + res);//all 的失败 1 })
- 参数有本人的 catch 时,状态为 fulfilled
- 参数中有一个 rejected 则会调用 all 上的 catch
- Promise.allSettled 会返回所有参数的状态与返回值
let p1 = Promise.resolve("胜利")
let p2 = "foo"
let p3 = Promise.reject("失败 1")
let p4 = Promise.reject("失败 2")
.catch(res => {console.log("本人的" + res);
return res
})
Promise.allSettled([p1, p2, p4])
.then(val => {console.log(val);
})
Promise.allSettled([p2, p4, p3])
.then(val => {console.log(val);
})
7.Promise.race()
- 相较于 Promise.all Promise.race 会返回一个 fulfilled 或 rejected 的参数
- Promise.race 返回的 Promise 的状态由跑的最快的 Promise 决定的
let p1 = new Promise((resolve, reject) => {setTimeout(resolve, 1000, "p1 承受")
})
let p2 = new Promise((resolve, reject) => {setTimeout(reject, 1500, "p2 回绝")
})
Promise.race([p1, p2])
.then(val => {console.log(val);//p1 承受
})
.catch(res => {console.log(res);
})
8.Promise.any()
-
Promise.any 正好与 Promise.all 相同,有一个 fulfilled 状态就为 fulfilled
let p1 = new Promise((resolve, reject) => {setTimeout(resolve, 2000, "p1 承受") }) let p2 = new Promise((resolve, reject) => {setTimeout(reject, 1500, "p2 回绝") }) let p3 = new Promise((resolve, reject) => {setTimeout(resolve, 1000, "p3 承受") }) Promise.any([p1, p2, p3]) .then(val => {console.log(val);//p3 承受 }) .catch(res => {console.log(res); })
- 如果全副为 rejected 则会抛出一个全副回绝的谬误
Promise.any([p2])
.then(val => {console.log(val);
})
.catch(res => {console.log(res);//AggregateError: All promises were rejected
})
9.Promise.try()
让同步函数同步执行
两种实现形式
// 形式一
Promise.try = function (func) {return (async () => func())()}
// 形式二
Promise.try = function (func) {return new Promise((resolve, reject) => {resolve(func())
})
}
- func()如果是
同步函数
会立刻执行
,如果是异步函数会将返回值或谬误交给 then 解决
let f = () => console.log("now");
Promise.try(f)
console.log("next");
//now
//next