状态
pending 等待
resolved 成功
rejected 失败
var p = new Promise((resolve,reject)=>{
//
setTimeout(()=>{
resolve()
})
})
setTimeout(()=>{
console.log(p)
})
console.log(p)
then和catch的返回值
then和catch返回值
1.都会返回promise
2.正常情况下状态为resolve,遇到错误状态为rejected
var p = Promise.resolve(100).then(res=>{
console.log(res)
throw "err"
})
p.then(()=>{
console.log(200)
}).catch(err=>{
console.log(err)
}).then(()=>{
console.log(300)
})
</script>
异常处理
(async () => {
var p = Promise.reject("err");
try {
let res = await p;
console.log(res)
}catch(err){
console.log(err)
}
})()
Promise.reject("err").catch((err)=>{
console.log(err)
})