[英]Nest JS resolving promises from Binance API
I am trying to create an api that connects into the Binance API using node and in particular NestJs.我正在尝试创建一个 api,它使用节点,特别是 NestJs 连接到 Binance API。
In my controller I have @Get('candles')
endpoint and call it by going to localhost:5000/candles.在我的 controller 中,我有
@Get('candles')
端点并通过转到 localhost:5000/candles 来调用它。 I am expecting it to return an object containing all the candleData retrieved from the Binance API.我希望它返回一个 object,其中包含从 Binance API 检索到的所有蜡烛数据。 I am using the npm package node-binance-api.
我正在使用 npm package node-binance-api。
I know the binance.candlesticks("BTCGBP", "4h", async (error, ticks, symbol) = {...
I am using is getting the data as inside I log the data received and show candleData.length in the console and it returns 100 candles as requested.我知道
binance.candlesticks("BTCGBP", "4h", async (error, ticks, symbol) = {...
我正在使用获取数据,因为我记录了接收到的数据并在控制台中显示 candleData.length并根据要求返回 100 支蜡烛。
My issue is resolving the promise that this npm package returns so that I can return it via the /candles endpoint.我的问题是解决 npm package 返回的 promise,以便我可以通过 /candles 端点返回它。 I am used to angular and observables and I am getting a bit confused by async and await.
我习惯了 angular 和 observables,我对 async 和 await 有点困惑。
app.controller.ts app.controller.ts
// Endpoint to fetch the object.
@Get('candles')
public getCandleData() {
console.log('Getting Candles');
let binanceResponse = this.getBinanceCandles().then(
(response) => {
const result = response
console.log('inside .then', result); // undefined
this.candles = result
return this.candles
}
);
console.log('@Get binanceResponse', binanceResponse); // Promise { <pending> }
return binanceResponse // undefined
}
// async function to retrieve data from Binance
async getBinanceCandles() {
let binanceResponse = await binance.candlesticks("BTCGBP", "4h", async (error, ticks, symbol) => {
if (error) return console.error(error);
const candleData = await ticks.map(candle => ({
time: candle[0],
open: candle[1],
high: candle[2],
low: candle[3],
close: candle[4]
}))
let last_tick = ticks[ticks.length - 1];
let [time, open, high, low, close, volume] = last_tick;
console.log('Length', candleData.length);
console.info(symbol + " last close: " + close);
return candleData
} , { limit: 100 })
return binanceResponse
}
console output控制台 output
Application is running on: http://[::1]:5000
Getting Candles
@Get binanceResponse Promise { <pending> }
inside .then undefined
Length 100
BTCGBP last close: 41628.85000000
I am not sure if I am using the await keyword correctly but clearly the promise is returning unresolved.我不确定我是否正确使用了 await 关键字,但显然 promise 返回未解决。 How do I get the endpoint localhost:5000/candles to return an object containing the 100 candles created in the getBinanceCandles() function?
如何让端点 localhost:5000/candles 返回包含在 getBinanceCandles() function 中创建的 100 根蜡烛的 object?
You are so close, just missing the async/await to wait for the value before return.你是如此接近,只是错过了 async/await 在返回之前等待值。
@Get('candles')
public async getCandleData() {
...
return await binanceResponse
}
Other approaches is to use await/async all the way:其他方法是一直使用等待/异步:
@Get('candles')
public async getCandleData() {
try {
console.log('Getting Candles');
const result = await this.getBinanceCandles()
this.candles = result // don't know why use this.candles here, i just keep it
return result
} catch(e) {
// TODO: handle error
}
}
Wrap the binance.candlesticks
in Promise to return value from callback将
binance.candlesticks
包装在 Promise 中以从回调中返回值
async getBinanceCandles() {
return new Promise((resolve, reject) => {
binance.candlesticks("BTCGBP", "4h", async (error, ticks, symbol) => {
if (error){ reject(error); }
const candleData = await ticks.map(candle => ({ // is there a need to use await?
time: candle[0],
open: candle[1],
high: candle[2],
low: candle[3],
close: candle[4]
}))
let last_tick = ticks[ticks.length - 1];
let [time, open, high, low, close, volume] = last_tick;
console.log('Length', candleData.length);
console.info(symbol + " last close: " + close);
resolve(candleData)
} , { limit: 100 })
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.