JS中使用Promise.all控制所有的非同步請求都完成後,在執行後續邏輯

2023-07-10 15:00:48
使用場景為,在js中連續的幾個非同步耗時操作,後面的耗時操作需要使用第一個操作的返回結果。例如呼叫ajax非同步介面,需要先建立完主表,然後拿到主表id在去迴圈建立明細表,等全部建立完成後,彈出提示來,或者失敗提示。
通常情況,在耗時操作完成後在去呼叫,需要將非同步方法轉成同步方法,結合回撥函數,像ajax請求可以直接設定為同步,然後再呼叫完成的回撥函數裡繼續去呼叫另一個邏輯程式碼,但這樣就會導致程式碼巢狀比較多,不易拓展和維護,使用Promise.all可以減少程式碼裡的不斷巢狀,直接上程式碼,具體的一看程式碼就明白了,promise的使用有單獨的筆記整理。
  1 QueryOrderInfo(orderId).then((orderResult) => {
  2             if (!orderResult.success) {
  3                 Xrm.Utility.alertDialog(`查詢所選訂單的資訊及明細資訊失敗!【${orderResult.message}】`);
  4                 return
  5             }
  6             let shippingId = commonUtil.delBrackets(Xrm.Page.data.entity.getId())
  7             let promiseArray = []
  8             orderResult.details.forEach((p, idx) => {
  9                 promiseArray.push(CreateShippingOrderDetailPromise(p, orderResult.order, shippingId))
 10             })
 11             Promise.all(promiseArray).then((res) => {
 12                 console.log(res);
 13                 let failReqs = Object.entries(res).filter((a) => !(a[1].success))
 14                 if (failReqs.length) {
 15                     Xrm.Utility.alertDialog(failReqs.map(e => e[1].message).join('\n'))
 16                 }
 17                 window.top.location.reload()
 18             })
 19         }).catch((err) => { console.error(err) })
 20         
 21         
 22 /**
 23  * 根據整車訂單id查詢整車銷售訂單明細
 24  * @param {any} orderId  整車訂單資料id
 25  */
 26 function QueryOrderInfo(orderId) {
 27     return new Promise((resolve, reject) => {
 28         let queryPromises = [GetOrderInfo(orderId), GetOrderDetails(orderId)]
 29         Promise.all(queryPromises).then((res) => {
 30             let orderRes = res[0]
 31             let detailRes = res[1]
 32             resolve({
 33                 success: res.every(r => r.success),
 34                 message: (orderRes.success ? "" : `[${orderRes.message}]`) + (detailRes.success ? "" : `[${detailRes.message}]`),
 35                 order: orderRes.data,
 36                 details: detailRes.data
 37             })
 38         })
 39     })
 40 }
 41 /**
 42  * 通過銷售訂單ID查詢銷售訂單的付款方式(new_paymentmethod)以及對應銷售合同的合同號(new_sales_contract_number)
 43  * @param {any} orderId
 44  * @returns
 45  */
 46 function GetOrderInfo(orderId) {
 47     return new Promise((resolve, reject) => {
 48         let queryUrl = `/new_orders(${orderId})?$select=new_paymentmethod&$expand=new_sales_contractid($select=new_sales_contract_number)`
 49         let result = { success: false, message: '未能正確查詢', data: null }
 50         commonUtil.queryWithUrl(queryUrl, function (res) {
 51             if (!res) {
 52                 result.message = '查詢銷售訂單資訊異常,新增訂艙資訊反饋單明細失敗!'
 53             } else if (!res.success) {
 54                 result.message = res.message
 55             } else if (res.data) {
 56                 result.success = true
 57                 result.data = {
 58                     new_paymentmethod: res.data["[email protected]"],
 59                     new_contractno: res.data.new_sales_contractid && res.data.new_sales_contractid.new_sales_contract_number
 60                 }
 61             }
 62             resolve(result)
 63         }, false);
 64     })
 65 }
 66 
 67 /**
 68  * 根據銷售訂單ID查詢相關的銷售訂單明細記錄
 69  * @param {any} orderId
 70  * @returns
 71  */
 72 function GetOrderDetails(orderId) {
 73     return new Promise((resolve, reject) => {
 74         let selFields = 'new_count,new_name,new_order_detailid,new_expected_output_time,_new_product_fourthid_value,_new_way_of_packagingid_value'
 75         let queryUrl = `/new_order_details?$select=${selFields}&$expand=new_way_of_packagingid($select=new_high,new_long,new_wide)&$filter=_new_order_value eq ${orderId}`
 76         let result = { success: false, message: '未能正確查詢', data: null }
 77         commonUtil.queryWithUrl(queryUrl, function (obj) {
 78             if (!obj) {
 79                 result.message = '查詢銷售訂單明細異常,新增訂艙資訊反饋單明細失敗!'
 80             } else if (!obj.success) {
 81                 result.message = obj.message
 82             } else if (obj.data && obj.data.length) {
 83                 result.success = true
 84                 result.data = obj.data
 85             }
 86             resolve(result)
 87         }, false);
 88     })
 89 }
 90 
 91 
 92 /**
 93  * 根據銷售訂單明細建立反饋單明細記錄
 94  * @param {any} orderDetail  銷售明細資料
 95  */
 96 function CreateShippingOrderDetailPromise(orderDetail, order,shippingId) {
 97     return new Promise((resolve, reject) => {
 98         //包裝方式
 99         var wayOfPackaging = orderDetail.new_way_of_packagingid
100         let shippingOrderDetail = {
101             "[email protected]": `/new_shipping_orders(${shippingId})`,       // 訂艙資訊反饋單ID
102             "[email protected]": `/new_order_details(${orderDetail.new_order_detailid})`,          // 整車銷售訂單明細
103             "[email protected]": `/new_product_fourths(${orderDetail._new_product_fourthid_value})`,  // 產品程式碼
104             new_numberofsets: orderDetail.new_count,                                                           // 臺數
105             new_timespan: new Date(orderDetail.new_expected_output_time).toISOString(),                        // 預計入庫時間
106             new_paymentmethod: order.new_paymentmethod,                                                        // 付款方式
107             new_contractno: order.new_contractno,                                                              // 合同號
108         }
109         if (wayOfPackaging) {
110             //包裝方式
111             shippingOrderDetail["[email protected]"] = `/new_way_of_packagings(${orderDetail._new_way_of_packagingid_value})`
112             //單臺長
113             shippingOrderDetail.new_singlelong = wayOfPackaging.new_long
114             //單臺寬
115             shippingOrderDetail.new_singlelwide = wayOfPackaging.new_wide
116             //單臺高
117             shippingOrderDetail.new_singlehigh = wayOfPackaging.new_high
118         }
119         Xrm.WebApi.createRecord("new_shipping_order_detail", shippingOrderDetail).then(
120             function success(result) {
121                 resolve({ success: true })
122             },
123             function (error) {
124                 resolve({ success: false, message: error.message })
125             }
126         );
127     })
128 }

思路就是,將所有的非同步耗時操作都封裝成Promise物件返回,Promise可以將非同步轉成同步,在執行完成後結果返回到then中,然後使用Promise.all將promise物件陣列一起執行,全部執行完成後再統一返回資訊。