When a catch((error) in this javascript code occures the whole application terminates
Hello!
I am quite new to javascript and I have created this code below.
The code works fine with no problems. However in this loop:
`
for (i = 0; i < exchanges.length; i++)
Where I iterate exchanges to retreive data from each exchange which is a Http Request. Sometimes the code fails to return the data through the Http Request.
const tickers = await exchange.fetchTickers();
`
When this happens, I catch this error in the console with this code:
catch((error) => { console.error(error); });
The problem now is that the Whole application terminates when the above code catches an error. I wonder how to prevent the application to terminate when an error is catched like this?
'use strict';
const ccxt = require('ccxt');
const fs = require('fs');
const path = require('path');
var chokidar = require('chokidar');
var watcher = chokidar.watch('C:/myproject/temp/tickers', { ignored: /^\./, persistent: true });
var i;
//Cache some memories first
var exchangesArray = [];
var exchangesNameArray = [];
(async () => {
const allexchanges = ccxt.exchanges.filter((id) => !['coinmarketcap', 'theocean'].includes(id))
.map(async (id) => {
const Exchange = ccxt[id];
const exchange = new Exchange({ enableRateLimit: true, 'timeout': 240000 });
if (exchange.has['fetchTickers']) {
exchangesArray.push(exchange);
exchangesNameArray.push(id);
}
});
await Promise.all(allexchanges);
})();
//Cached Memories
const exchanges = exchangesArray; //Holds exchange object
const exchangesnames = exchangesNameArray; //Holds exchange name
//**************************************************************
//FETCH ALL TICKERS
//**************************************************************
//File Watcher
watcher.on('add', function (path2) {
//Use cached memories to do the "fetchTickers()" as fast as possible
var exchangesToLogArray = fs.readFileSync('C:/myproject/temp/file1.txt', 'utf8').toString().split('\n').toString().trim();
var i;
(async () => {
console.log(`start`);
const start = Date.now();
//Use cached memories to do the "fetchTickers()" as fast as possible
var tickersPromises = []
for (i = 0; i < exchanges.length; i++) { //Only log choose exchanges
// pop one exchange from the array
if (exchangesToLogArray.includes(exchangesnames[i])) {
console.log(exchangesnames[i]);
const exchange = exchanges[i]
const exchangename = exchangesnames[i]
//try {
let tickerProcessing = new Promise(async (resolve) => {
// probably do a try catch in here
const tickers = await exchange.fetchTickers();
const dumpFile = path.join('C:/myproject/tickers', `${exchangename}Tickers.txt`);
await fs.promises.writeFile(dumpFile, JSON.stringify(tickers));
resolve()
}).catch((error) => { console.error(error); });
tickersPromises.push(tickerProcessing)
//} catch (e) { console.error(e.exchange); }
}
}
// wait for all of them to execute or fail
await Promise.all(tickersPromises)
const end = Date.now();
await fs.promises.writeFile('C:/myproject/temp/readyflag.txt', 'dummy'); //readyflagfile when all tickers has been downloaded!
console.log(`Tickers in ${(end - start) / 1000} seconds`);
})();
});