1、 pending
var p = new Promise((resolve,reject)=>{
})
2、fulfilled
var f = new Promise((resolve,reject)=>{
resolve(10);
})
3、rejected
var r = new Promise((resolve,reject)=>{
reject(20);
})
二、状态的变化
状态的变化是不可逆的
pending --> fulfilled
var p = new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve(10);
})
pending --> rejected
var r = new Promise((resolve,reject)=>{
setTimeout(()=>{
reject(20)
},2000)
})
三、状态的触发
resolve then -->触发之后的结果是Promise,状态fulfilled
reject catch -->触发之后的结果是Promise,状态fulfilled
var p = Promise.resolve(10);
var r = Promise.reject(20);
console.log(p)
var result = p.then(res=>{
console.log(res)
})
console.log(result)
var result02 = r.catch(res => {
console.log(res)
})
console.log(result02)
四、then和catch返回值的状态的变化
1-1 pending—>resolve
var p = Promise.resolve(100).then(res=>{
console.log(res)
})
console.log(p) //Promise fulfilled
1-2 then函数中报错,返回值的状态为rejected
var p = Promise.resolve(100).then(res=>{
console.log(res);
throw "error"
})
console.log(p) //Promise rejected
1-3 then函数返回值报错的处理
var p = Promise.resolve(10).then(res=>{
console.log(res);
throw "error"
})
/*
p Promise rejected
*/
var res = p.then(()=>{
console.log("20");
}).catch(res=>{
console.log(res)
})
console.log(res)
1-4 catch之后的报错
var r = Promise.reject(10).catch(res=>{
console.log(res); //10
throw "error";
})
r.then(()=>{
console.log(200);
}).catch(err=>{
console.log(err);
})