gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
const openESignRequest = (cmrId) => { let otherData = {};…
- User
const openESignRequest = (cmrId) => { let otherData = {}; getCMRDetailsById(cmrId).then((otherData) => { EMGRSignUtils.sendESignDocuments('CMR', cmrId, otherData); }) };
const getCMRDetailsById = async (cmrId) => { let url = AppContext.restUrl + 'CMR/getCMRDetailById'; $(".gridloading").show(); $.ajax({ url: url, type: 'POST', data: {"data": Base64.encode(JSON.stringify({"cmrId": cmrId}))}, success: function (response) { $(".gridloading").hide(); console.log(response); return response; }, error: function (jqXHR, textStatus, errorThrown) { emgrToast.error("Error while retrieving data!!", 5000); $(".gridloading").hide(); } }); }please refactor this code
- Assistant
Certainly! Here's a refactored version of your JavaScript code that improves readability, uses
async/awaitfor handling asynchronous calls, and addresses some potential issues.const openESignRequest = async (cmrId) => { try { const otherData = await getCMRDetailsById(cmrId); EMGRSignUtils.sendESignDocuments('CMR', cmrId, otherData); } catch (error) { console.error("Error opening e-sign request:", error); } }; const getCMRDetailsById = async (cmrId) => { const url = `${AppContext.restUrl}CMR/getCMRDetailById`; $(".gridloading").show(); try { const response = await $.ajax({ url: url, type: 'POST', data: { "data": Base64.encode(JSON.stringify({ "cmrId": cmrId })) }, }); console.log(response); return response; } catch (error) { emgrToast.error("Error while retrieving data!!", 5000); console.error("Error fetching CMR details:", error); throw error; // Re-throw error for handling in caller function } finally { $(".gridloading").hide(); } };