function tool(count, successCall, failCall) {
setTimeout(() => {
if (count > 0) {
//填写业务逻辑
successCall();
} else {
//填写业务逻辑
failCall();
}
}, 2000);
}
tool(
10,
() => {
//成功之后的业务逻辑
console.log("执行成功");
},
() => {
//失败之后的业务逻辑
console.log("执行失败");
}
);
function tool(count) {
//创建promise对象
const promise = new Promise((resolve, reject) => {
if (count > 0) {
//成功时候执行resolve
resolve("成功");
} else {
//失败时候执行reject
reject("失败");
}
});
//返回promise对象
return promise;
}
tool(10)
//.then接收resolve执行函数
.then((res) => {
console.log(res);
})
//.catch接收reject执行的函数
.catch((err) => {
console.log(err);
});
pending(等待),fulfilled(成功)、rejected(已拒绝)
const promise = new Promise((resolve, reject) => {
console.log(123);
console.log(123);
///--以上都是pending状态
resolve("123"); //转成了fulfilled状态
resolve("adafaf");//只会执行一次改变状态的函数
reject(); //不会执行reject
console.log(123456); //但是后面的代码会执行
});
promise
.then((res) => {
console.log(res);
})
.catch((err) => {});
//传递普通值
const promise1 = new Promise((resolve, reject) => {
resolve("123");
});
promise1.then((res) => {
console.log(res);
});
//传递另外一个promise
const p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(123);
}, 2000);
});
const promise2 = new Promise((resolve, reject) => {
resolve(p);
});
promise2.then((res) => {
console.log(res);
});
//传递的对象中,有then方法
const promise3 = new Promise((resolve, reject) => {
resolve({
a: 100,
//then中也要有resolve参数
then(resolve, reject) {
resolve(2222);
},
});
});
promise3.then((res) => {
console.log(res);
});
const promise1 = new Promise((resolve, reject) => {
reject("123");
});
promise1.then((res) => {
console.log(res);
}).catch((err)=>{
//使用catch接收
console.log(err);
})
//若没有catch进行接收,则会报错
const promise1 = new Promise((resolve, reject) => {
reject("123");
});
promise1.then((res) => {
console.log(res);
});
return的返回值,会让then方法中的promise处于fufilled状态,可以被后面的then接收到
throw new Error 会让then方法中的promise处于rejected状态,可以被后面最近的catch接收到
const promise1 = new Promise((resolve, reject) => {
reject("123");
});
promise1.then(res=>{
console.log(res)
}).catch(err=>{
//因为前面的then方法返回的是promise,所以后面可以接catch,进行链式调用
console.log(err)
})
const p = new Promise((resolve, reject) => {
resolve(789);
});
const promise1 = new Promise((resolve, reject) => {
resolve("123");
});
promise1
.then((res) => {
console.log(res);
//返回一个普通值
return 456;
})
//前一个then会创建一个新的Promise,并将return的内容,传递给新创建的Promise的reslove
//所以这个then会接收到前一个then的return值
.then((res) => {
console.log(res); //
//返回一个promise
//相当于resolve(p),所以状态会由p决定
return p;
})
.then((res) => {
console.log(res); //789
//return一个对象,该对象中由then方法
return {
then(resolve) {
resolve("hahaha");
},
};
})
.then((res) => {
console.log(res); //hahaha
});
与then相同,返回值是promise对象
当catch中,return值时,新创建的promise处于fulfilled状态
当catch中,throw new Error值时,新创建的promise处于rejected状态
const promise1 = new Promise((resolve, reject) => {
reject(123);
});
promise1
.then((res) => {
console.log(res);
return 123;
})
.then((res) => {
console.log(res);
})
.then((res) => {
console.log(res);
})
//这里的catch接收的不是前面then中promise的reject状态,而是接收的promise1的reject
.catch((err) => {
console.log(err);
});
是在ES9中新增的特性,表示无论Promise对象无论是fulfilled还是rejected状态,都会执行该方法
const promise1 = new Promise((resolve, reject) => {
reject(123);
});
promise1
.then((res) => {
console.log(res);
return 123;
})
.catch((err) => {
console.log(err);
})
.finally(() => {
console.log("我肯定会执行");
});
let count = 0
const promise1 = new Promise((resolve, reject) => {
if(count === 0){
//这就是类方法
reject(123);
}else{
//这就是类方法
resolve(456)
}
});
resolve里面的值有三种情况(上面有介绍)
reject里面的值不分情况,都会作为reject状态参数,传递到catch中
如果有一个已经存在的值,想直接返回fulfilled或者rejected状态,可以用以下写法
let obj = {a:100}//想将这个值转成promise的fulfilled状态
const p = Promise.resolve(obj)
p.then((res)=>{
console.log(res)
})
//想把这个值转成rejected状态
const p1 = Promise.reject(obj)
p1.catch((err)=>{
console.log(err)
})
传入多个Promise,将多个Promise包裹成一个新的Promise
const promise1 = new Promise((resolve, reject) => {
resolve(123);
});
const promise2 = new Promise((resolve, reject) => {
resolve(456);
});
const promise3 = new Promise((resolve, reject) => {
resolve(789);
});
//all方法中传入的参数,必须是可迭代的(后续文章有奖可迭代对象)
Promise.all([promise1, promise2, promise3]).then((res) => {
console.log(res); //[123,456,789],是一个数组形式,包含里面的所有Promise的返回值
});
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(123);
}, 1000);
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(456);
}, 500);
});
const promise3 = new Promise((resolve, reject) => {
resolve(789);
});
//all方法中传入的参数,必须是可迭代的(后续文章有奖可迭代对象)
Promise.all([promise1, promise2, promise3])
.then((res) => {
console.log(res); //不会执行then中的方法
})
.catch((err) => {
console.log(err);//456,因为promise2是最先为rejected状态的,同时promise1根本不会执行
});
上面我们知道,all方法存在一定的缺陷,因为只要有一个rejected状态,该状态后面的promise都不会执行
而allSettled解决了这个问题
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(123);
}, 1000);
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(456);
}, 500);
});
const promise3 = new Promise((resolve, reject) => {
resolve(789);
});
//all方法中传入的参数,必须是可迭代的(后续文章有奖可迭代对象)
Promise.allSettled([promise1, promise2, promise3]).then((res) => {
console.log(res);
});
/**
[
{ status: 'rejected', reason: 123 },
{ status: 'rejected', reason: 456 },
{ status: 'fulfilled', value: 789 }
]
*/
谁先有结果,就会使用谁的结果,不会等到所有的Promise都有结果
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(123);
}, 1000);
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(456);
}, 500);
});
const promise3 = new Promise((resolve, reject) => {
reject(789);
});
//all方法中传入的参数,必须是可迭代的(后续文章有奖可迭代对象)
Promise.race([promise1, promise2, promise3])
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);//789
});
与race方法不同,该方法会等待第一个fulfilled状态的Promise返回值
若都是rejected状态,则会再catch中返回All Promise were rejected
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(123);
}, 1000);
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(456);
}, 500);
});
const promise3 = new Promise((resolve, reject) => {
reject(789);
});
//all方法中传入的参数,必须是可迭代的(后续文章有奖可迭代对象)
Promise.any([promise1, promise2, promise3])
.then((res) => {
console.log(res);//123
})
.catch((err) => {
console.log(err);
});
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(123);
}, 1000);
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(456);
}, 500);
});
const promise3 = new Promise((resolve, reject) => {
reject(789);
});
//all方法中传入的参数,必须是可迭代的(后续文章有奖可迭代对象)
Promise.any([promise1, promise2, promise3])
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);///AggregateError: All promises were rejected
});