我正在使用以下两个文件从两个 API 获取数据。请在下面找到我的最小可行示例:
poloniex.js
const Poloniex = require('poloniex-api-node')
const poloniex = new Poloniex()
async function getExchangeTicker() {
poloniex.returnTicker((err, ticker) => {
if (err) {
console.log(err.message)
} else {
//console.log(ticker)
return ticker
}
})
}
module.exports = {
getExchangeTicker,
}
cctx.js
const ccxt = require ('ccxt')
async function getExchangeTicker() {
const bitfinex = new ccxt.bitfinex({ verbose: true })
const data = await bitfinex.fetchTicker()
return data
}
module.exports = {
getExchangeTicker,
}
调度程序.js
const exchangePoloniex = require('../exchange/poloniex')
const exchangeCCTX = require('../exchange/cctx')
async function getAllTickers() {
const exchanges = [
exchangePoloniex,
exchangeCCTX,
]
let res
exchanges.forEach((exchange) => {
res = exchange.getExchangeTicker()
})
return res
}
async function runScheduler() {
let res
setInterval(() => {
this.res = getAllTickers()
}, 3000)
console.log("res: " + res)
return res
}
runScheduler()
我正在运行一个调度程序来汇集这些文件中的数据,但只能res: undefined
返回。
关于如何从这两个 API 中正确获取数据的任何建议?
我非常感谢您的回复!