Фикс рекурсии

This commit is contained in:
Данил 2024-10-21 20:57:46 +03:00
parent 75a97ff2ca
commit 14b7420557

View file

@ -3,16 +3,14 @@ const axios = require('axios');
const config = require('../shared/config/src/main.js')(); const config = require('../shared/config/src/main.js')();
const logger = require('../shared/logger/src/main.js'); const logger = require('../shared/logger/src/main.js');
/** async function save_fiat(depth = 0) {
* Saves exchange rate of the fiat currency
* @returns {Object} -
*/
async function save_fiat() {
if (!config['currency']['collecting']['fiat']) return; if (!config['currency']['collecting']['fiat']) return;
const max_depth = 5;
config['currency']['fiat'].forEach((value) => config['currency']['fiat'].forEach((value) =>
config['currency']['fiat'].forEach(async (pair) => { config['currency']['fiat'].forEach(async (pair) => {
if (value === pair) return; if (value === pair) return;
await axios await axios
.get( .get(
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`, `https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`,
@ -55,12 +53,19 @@ async function save_fiat() {
], ],
); );
}) })
.catch((err) => { .catch(async (err) => {
logger.error(err); logger.error(err);
setTimeout(
save_fiat, if (depth < max_depth) {
err.config?.timeout ? err.config?.timeout : 3000, await new Promise((resolve) =>
); setTimeout(resolve, max_depth),
);
await save_fiat(depth + 1);
} else {
logger.error(
'Max retry limit reached for saving fiat data.',
);
}
}); });
}), }),
); );