如果我在该函数之外移动socket.emit,则会发生套接字发出事件。但是,我需要掌握“价值观”。数据,在承诺回电中。在这种情况下,套接字发出事件并不会发生。
app.io.on('connection', function(socket) {
setInterval(function() {
var bitfinex = new Promise(
function(resolve, reject) {
const orderBook = 'https://api.bitfinex.com/v2/book/tBTCUSD/P0'
axios.get(orderBook)
.then(function(response) {
resolve(response)
})
.catch(function(error) {
reject(error)
});
});
var bitmex = new Promise(
function(resolve, reject) {
const orderBook = 'https://www.bitmex.com/api/v1/orderBook/L2?symbol=xbt&depth=25'
axios.get(orderBook)
.then(function(response) {
resolve(response)
})
.catch(function(error) {
reject(error)
});
});
Promise.all([bitmex, bitfinex]).then(values => {
socket.emit('feed', {
data: values
})
});
}, 3000)
答案 0 :(得分:0)
Axios正在返回一个承诺,所以你不需要创建新的Promise,如果有的话你应该抓住你的错误:
app.io.on(
'connection',
function (socket) {
setInterval(
function () {
Promise.all(
[
axios.get('https://api.bitfinex.com/v2/book/tBTCUSD/P0'),
axios.get('https://www.bitmex.com/api/v1/orderBook/L2?symbol=xbt&depth=25')
]
).then(values =>
socket.emit(
'feed',
{
data: values
}
)
)
.catch(
e => console.warn("something went wrong:",e)
)
},
3000
)
}
);