From bc9ece59d3e79aac53f877d0d44a6acadf15c726 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 4 Feb 2021 13:52:31 +0100 Subject: [PATCH] Moving function to utilities --- .../Low-Frequency-Trading/TradingProcess.js | 24 +++---------------- .../TS/Utilities/MiscellaneousFunctions.js | 24 ++++++++++++++++++- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingProcess.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingProcess.js index e90aa69fd..2ff2ba712 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingProcess.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingProcess.js @@ -337,7 +337,7 @@ let filePath = datasetModule.node.parentNode.config.codeName + '/' + datasetModule.node.config.codeName /* We cut the async calls via callBacks at this point, so as to have a clearer code upstream */ - let response = await asyncGetDatasetFile(datasetModule, filePath, fileName) + let response = await TS.projects.superalgos.utilities.miscellaneousFunctions.asyncGetDatasetFile(datasetModule, filePath, fileName) if (response.err.message === 'File does not exist.') { TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, @@ -429,7 +429,7 @@ let filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel /* We cut the async calls via callBacks at this point, so as to have a clearer code upstream */ - let response = await asyncGetDatasetFile(datasetModule, filePath, fileName) + let response = await TS.projects.superalgos.utilities.miscellaneousFunctions.asyncGetDatasetFile(datasetModule, filePath, fileName) if (response.err.message === 'File does not exist.') { TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, @@ -582,7 +582,7 @@ let fileName = "Data.json" /* We cut the async calls via callBacks at this point, so as to have a clearer code upstream */ - let response = await asyncGetDatasetFile(datasetModule, filePath, fileName) + let response = await TS.projects.superalgos.utilities.miscellaneousFunctions.asyncGetDatasetFile(datasetModule, filePath, fileName) if (response.err.message === 'File does not exist.') { TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, @@ -933,22 +933,4 @@ } } } - - async function asyncGetDatasetFile(datasetModule, filePath, fileName) { - - let promise = new Promise((resolve, reject) => { - - datasetModule.getTextFile(filePath, fileName, onFileReceived) - function onFileReceived(err, text) { - - let response = { - err: err, - text: text - } - resolve(response) - } - }) - - return promise - } } diff --git a/Projects/Superalgos/TS/Utilities/MiscellaneousFunctions.js b/Projects/Superalgos/TS/Utilities/MiscellaneousFunctions.js index 22d951878..568b37dd3 100644 --- a/Projects/Superalgos/TS/Utilities/MiscellaneousFunctions.js +++ b/Projects/Superalgos/TS/Utilities/MiscellaneousFunctions.js @@ -3,7 +3,8 @@ exports.newSuperalgosUtilitiesMiscellaneousFunctions = function () { let thisObject = { genereteUniqueId: genereteUniqueId, truncateToThisPrecision: truncateToThisPrecision, - pad: pad + pad: pad, + asyncGetDatasetFile: asyncGetDatasetFile } return thisObject @@ -23,4 +24,25 @@ exports.newSuperalgosUtilitiesMiscellaneousFunctions = function () { str = str.toString(); return str.length < max ? pad("0" + str, max) : str; } + + async function asyncGetDatasetFile(datasetModule, filePath, fileName) { + /* + This function helps a caller to use await syntax while the called + function uses callbacks, specifically for retrieving files. + */ + let promise = new Promise((resolve, reject) => { + + datasetModule.getTextFile(filePath, fileName, onFileReceived) + function onFileReceived(err, text) { + + let response = { + err: err, + text: text + } + resolve(response) + } + }) + + return promise + } } \ No newline at end of file