# 多次异步

应用场景,在购买前,查询是否有购买权限。


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;
};
  1. 将需要查询的对象goodsBizNos遍历,创建请求队列
  2. Promise.all,请求队列
  3. 在返回的responses(数组)中查找是否有异常数据,此时用find
  4. 处理第一个异常数据current