we are using mocha + nodejs + axios
Below is the piece of code i have , they are all in different file.
File 1 : IT block
it('Perform Post Payment Actions', function () {
postPaymentController.triggerPostPaymentActions(params);
});
File 2 : Controller
module.exports.triggerPostPaymentActions = function (params) {
const actions = params.postAuthActionId.split('-'); // // 'PARTIALREFUND, 50, SUCCESS - PARTIALREFUND, 50, FAIL
Object.entries(actions).forEach(([, action]) => {
const actionAsArray = action.split(','); // 'PARTIALREFUND, 50, SUCCESS
switch (actionAsArray[0]) {. // PARTIALREFUND
case 'PARTIALREFUND':
paymentsPartialRefundApi.submitPaymentsPartialRefundApi(actionAsArray, params);
break;
default:
break;
}
});
};
File 3: Acutal API Call using axios
module.exports.submitPaymentsPartialRefundApi = async function (actionDetails, params) {
const postOptions = {
url: partialRefundApiUri,
method: 'POST',
headers: restHeaders,
data: params.partialRefundRequest,
};
try {
const response = await axios(postOptions);
params.partialRefundResponse = response.data;
process.env.PARTIALREFUNDRESPONSE = JSON.stringify(params.partialRefundResponse);
} catch (err) {
process.env.ERRORCODE = err;
process.env.SCRIPT_STATUS = 'failure';
process.env.PARTIALREFUNDRESPONSE = 'Partial Refund request Failed';
throw err;
}
};
PROBLEM :
When the control is passed to submitPaymentsPartialRefundApi , then it builds the request for axios but rather than waiting for the axios call , it takes the control back to the switch case. my expectation was that since i am using async await it will execute and wait till the axios call is completed but rather than completing the call it returns the control back to the switch statement and from the switch it goes back to the IT block. so i have 2 problems.
1 Its not waiting for the axios call to end
2 Its not running switch twice in loop , its goes back to IT block and then comes back.
Solutions Tried :
I already tried using promises for this , with and without timeout but still it did not worked.
Any help on this is appreciated.