From b616725259ab2e5d2b6a498af55330273e41a214 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Fri, 25 Mar 2022 07:06:35 +0300 Subject: [PATCH 1/2] nest promise chain --- src/libs/actions/IOU.js | 49 +++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 686cb51e15e0..18cccaccf9f7 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -138,35 +138,40 @@ function createIOUSplit(params) { emailList: _.map(params.splits, participant => participant.email).join(','), }) .then((response) => { + if (response.jsonCode !== 200) { + processIOUErrorResponse(response); + return; + } + chatReportID = response.reportID; return API.CreateIOUSplit({ ...params, splits: JSON.stringify(params.splits), reportID: response.reportID, + }) + .then((response) => { + if (response.jsonCode !== 200) { + processIOUErrorResponse(response); + return; + } + + // This data needs to go from this: + // {reportIDList: [1, 2], chatReportIDList: [3, 4]} + // to this: + // [{reportID: 1, chatReportID: 3}, {reportID: 2, chatReportID: 4}] + // in order for getIOUReportsForNewTransaction to know which IOU reports are associated with which + // chat reports + const reportParams = []; + for (let i = 0; i < response.reportIDList.length; i++) { + reportParams.push({ + reportID: response.reportIDList[i], + chatReportID: response.chatReportIDList[i], + }); + } + getIOUReportsForNewTransaction(reportParams); + Navigation.navigate(ROUTES.getReportRoute(chatReportID)); }); }) - .then((response) => { - if (response.jsonCode !== 200) { - processIOUErrorResponse(response); - return; - } - - // This data needs to go from this: - // {reportIDList: [1, 2], chatReportIDList: [3, 4]} - // to this: - // [{reportID: 1, chatReportID: 3}, {reportID: 2, chatReportID: 4}] - // in order for getIOUReportsForNewTransaction to know which IOU reports are associated with which - // chat reports - const reportParams = []; - for (let i = 0; i < response.reportIDList.length; i++) { - reportParams.push({ - reportID: response.reportIDList[i], - chatReportID: response.chatReportIDList[i], - }); - } - getIOUReportsForNewTransaction(reportParams); - Navigation.navigate(ROUTES.getReportRoute(chatReportID)); - }); } /** From 63704458960aeab0550ca2fab485b15f872bc6b5 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Fri, 25 Mar 2022 07:51:42 +0300 Subject: [PATCH 2/2] undo nest promise --- src/libs/actions/IOU.js | 47 ++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 18cccaccf9f7..b7ce3d7c2e34 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -139,8 +139,7 @@ function createIOUSplit(params) { }) .then((response) => { if (response.jsonCode !== 200) { - processIOUErrorResponse(response); - return; + return response; } chatReportID = response.reportID; @@ -148,30 +147,30 @@ function createIOUSplit(params) { ...params, splits: JSON.stringify(params.splits), reportID: response.reportID, - }) - .then((response) => { - if (response.jsonCode !== 200) { - processIOUErrorResponse(response); - return; - } - - // This data needs to go from this: - // {reportIDList: [1, 2], chatReportIDList: [3, 4]} - // to this: - // [{reportID: 1, chatReportID: 3}, {reportID: 2, chatReportID: 4}] - // in order for getIOUReportsForNewTransaction to know which IOU reports are associated with which - // chat reports - const reportParams = []; - for (let i = 0; i < response.reportIDList.length; i++) { - reportParams.push({ - reportID: response.reportIDList[i], - chatReportID: response.chatReportIDList[i], - }); - } - getIOUReportsForNewTransaction(reportParams); - Navigation.navigate(ROUTES.getReportRoute(chatReportID)); }); }) + .then((response) => { + if (response.jsonCode !== 200) { + processIOUErrorResponse(response); + return; + } + + // This data needs to go from this: + // {reportIDList: [1, 2], chatReportIDList: [3, 4]} + // to this: + // [{reportID: 1, chatReportID: 3}, {reportID: 2, chatReportID: 4}] + // in order for getIOUReportsForNewTransaction to know which IOU reports are associated with which + // chat reports + const reportParams = []; + for (let i = 0; i < response.reportIDList.length; i++) { + reportParams.push({ + reportID: response.reportIDList[i], + chatReportID: response.chatReportIDList[i], + }); + } + getIOUReportsForNewTransaction(reportParams); + Navigation.navigate(ROUTES.getReportRoute(chatReportID)); + }); } /**