# 多次异步
应用场景,在购买前,查询是否有购买权限。
const handleGoodBuy = async ({ goodsSn, goodsBizNos, skuSn }) => {
Toast.loading();
try {
const isSingle = goodsBizNos.length === 1;
const responses = await Promise.all(goodsBizNos.map(goodsBizNo => courseService.checkPurchased(goodsBizNo)));
const current = responses.find(response => Object.keys(response).length > 0);
Toast.hide();
if (current) {
PreventPurchaseBox.show({ courseName: current.title, isSingle });
return false;
}
window.location.href = `/finance/course/cashier?goodsSn=${goodsSn}&skuSn=${skuSn}&flowId=${flowId}`;
} catch (error) {
Toast.show(error.message);
}
return true;
};
- 将需要查询的对象goodsBizNos遍历,创建请求队列
- Promise.all,请求队列
- 在返回的responses(数组)中查找是否有异常数据,此时用find
- 处理第一个异常数据current