我有这段代码,我尝试在使用它们通过库执行 HTTP 请求之前缓存必要的内存。我之前缓存它们以使代码在实际执行请求时更快,这对于尽可能快地进行至关重要。
该代码正在运行,但现在它以“同步”方式处理请求。
我相信问题代码是以下行:
for (i = 0; i < exchanges.length; i++)
我不确定运行上述 for 循环异步的最佳/最快方法是什么?
'use strict';
const ccxt = require('ccxt');
const fs = require('fs');
const path = require('path');
//Cache some memories first
var exchangesArray = [];
var exchangesArray2 = [];
(async () => {
const allexchanges = ccxt.exchanges.filter((id) => !['coinmarketcap', 'theocean'].includes(id))
.map(async (id) => {
const Exchange = ccxt[id];
const exchange = new Exchange({ enableRateLimit: true });
if (exchange.has['fetchTickers']) {
exchangesArray.push(exchange);
exchangesArray2.push(id);
}
});
await Promise.all(allexchanges);
})();
//The cached memories
const exchanges = exchangesArray; //Holds exchanges object
const exchangesnames = exchangesArray2; //Holds exchanges name
var i;
//Use cached memories to do the "fetchTickers()" as fast as possible now
(async () => {
console.log(`start`);
const start = Date.now();
//What is the fastest way to make this for loop async/await to run in parallel?
for (i = 0; i < exchanges.length; i++) {
// pop one exchange from the array
const exchange = exchanges[i]
const exchangename = exchangesnames[i]
try {
const tickers = await exchange.fetchTickers();
const dumpFile = path.join(__dirname, 'tickers', `${exchangename}Tickers.json`);
await fs.promises.writeFile(dumpFile, JSON.stringify(tickers));
} catch (e) {
console.error(e);
}
}
// wait for all of them to execute or fail
await Promise.all(exchanges)
const end = Date.now();
console.log(`Done in ${(end - start) / 1000} seconds`);
})();