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

    1. const handleGoodBuy = async ({ goodsSn, goodsBizNos, skuSn }) => {
    2. Toast.loading();
    3. try {
    4. const isSingle = goodsBizNos.length === 1;
    5. const responses = await Promise.all(goodsBizNos.map(goodsBizNo => courseService.checkPurchased(goodsBizNo)));
    6. const current = responses.find(response => Object.keys(response).length > 0);
    7. Toast.hide();
    8. if (current) {
    9. PreventPurchaseBox.show({ courseName: current.title, isSingle });
    10. return false;
    11. }
    12. window.location.href = `/finance/course/cashier?goodsSn=${goodsSn}&skuSn=${skuSn}&flowId=${flowId}`;
    13. } catch (error) {
    14. Toast.show(error.message);
    15. }
    16. return true;
    17. };
    1. 将需要查询的对象goodsBizNos遍历,创建请求队列
    2. Promise.all,请求队列
    3. 在返回的responses(数组)中查找是否有异常数据,此时用find
    4. 处理第一个异常数据current