NodeJS等待请求返回正文而不是函数结果

时间:2018-02-17 14:18:11

标签: node.js async-await

我希望console.log(price)打印' =>'之后的函数返回值,但是我打印了请求的正文。

"use strict";
const request = require('request-promise');
const PromedioPonderado = require('../PromedioPonderado.js');
var exports = module.exports = {};

exports.GetPrices = async function(){
    var price = await request('https://www.okex.com/api/v1/depth.do?symbol=btc_usdt', { json: true }, (err, res, body) => {
        if (err) { return console.log(err); }
        var promedio = new PromedioPonderado();
        var bids = body.bids;
        for (var i = 0, len = bids.length; i < len; i++) {
            var row = bids[i];
            var bid = {Price: row[0], Amount: row[1]}
            promedio.bid(bid);
        }
        var asks = body.asks;
        for (var i = 0, len = asks.length; i < len; i++) {
            var row = asks[i];
            var ask = {Price: row[0], Amount: row[1]}
            promedio.ask(ask);
        }
        var askReturn = promedio.askAverage(); //sync function
        var bidReturn = promedio.bidAverage(); // sync function
        console.log(askReturn)
        return {Ask: askReturn, Bid: bidReturn}; //I want to return this value
    });
    console.log(price);
}

这是PromedioPonderado.js,以防万一

"use strict";
class PromedioPonderado {
  constructor() {
    this._bid = [];
    this._ask = [];
  }
  bid(bid) {
    this._bid.push(bid);
  }
  ask(ask){
    this._ask.push(ask);
  }
  bidAverage(){
    var totalAmount = 0;
    var i = 0;
    var average = 0;
    while(totalAmount < 10){
        totalAmount = totalAmount + this._bid[i].Amount;
        average = average + (this._bid[i].Price * this._bid[i].Amount);
        i++;
    }
    average = average / (totalAmount);
    return average.toFixed(2);
  }
  askAverage(){
    var totalAmount = 0;
    var i = 0;
    var average = 0;
    while(totalAmount < 10){
        totalAmount = totalAmount + this._ask[i].Amount;
        average = average + (this._ask[i].Price * this._ask[i].Amount);
        i++;
    }
    average = average / (totalAmount);
    return average.toFixed(2);
  }
}
module.exports = PromedioPonderado;

2 个答案:

答案 0 :(得分:1)

var body = await request('https://www.okex.com/api/v1/depth.do?symbol=btc_usdt', { json: true }

然后您可以使用请求中的响应。

答案 1 :(得分:1)

exports.GetPrices = async function(){
    var price = await new Promise((resolve, reject) => {
      request('https://www.okex.com/api/v1/depth.do?symbol=btc_usdt', { json: true }, (err, res, body) => {
        if (err) { reject(err); }
        var promedio = new PromedioPonderado();
        var bids = body.bids;
        for (var i = 0, len = bids.length; i < len; i++) {
            var row = bids[i];
            var bid = {Price: row[0], Amount: row[1]}
            promedio.bid(bid);
        }
        var asks = body.asks;
        for (var i = 0, len = asks.length; i < len; i++) {
            var row = asks[i];
            var ask = {Price: row[0], Amount: row[1]}
            promedio.ask(ask);
        }
        var askReturn = promedio.askAverage(); //sync function
        var bidReturn = promedio.bidAverage(); // sync function
        console.log(askReturn)
        resolve({Ask: askReturn, Bid: bidReturn}); //I want to return this value
    });
    })
    console.log(price);
}