Nest JS 解析来自 Binance 的承诺 API

Nest JS resolving promises from Binance API

我正在尝试创建一个 api 连接到 Binance API 使用节点,特别是 NestJs。

在我的控制器中,我有 @Get('candles') 端点并通过转到 localhost:5000/candles 调用它。我期待它 return 一个包含从 Binance API 检索到的所有 candleData 的对象。我正在使用 npm 包 node-binance-api.

我知道我正在使用的 binance.candlesticks("BTCGBP", "4h", async (error, ticks, symbol) = {... 正在获取内部数据 我记录收到的数据并在控制台中显示 candleData.length 并且它 return 根据要求显示 100 支蜡烛。

我的问题是解决这个 npm 包 returns 的承诺,这样我就可以通过 /candles 端点 return 它。我习惯了 angular 和 observables,我对 async 和 await 有点困惑。

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
}

控制台输出

Application is running on: http://[::1]:5000
Getting Candles
@Get binanceResponse Promise { <pending> }
inside .then undefined
Length 100
BTCGBP last close: 41628.85000000

我不确定我是否正确使用了 await 关键字,但显然承诺 returning 未解决。我如何将端点 localhost:5000/蜡烛 return 包含在 getBinanceCandles() 函数中创建的 100 根蜡烛的对象?

你太接近了,只是缺少 async/await 等待 return 之前的值。

@Get('candles')
public async getCandleData() {
   ...
   return await binanceResponse
}

其他方法是一直使用await/async:

@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
    }
}

将 Promise 中的 binance.candlesticks 包装为来自回调的 return 值

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 })
    })
  }