承诺已收到待处理

时间:2018-08-02 16:48:45

标签: javascript node.js function es6-promise

此代码返回未定的承诺 我想将insta函数返回的数据存储到数据数组中。

cs2.js:- 
    async function insta(mode, coin) {
      coin = coin.toLowerCase();
      var price;
      var quantity;
      await new Promise(function (resolve, reject) {
        request.get(url, function (err, response, body) {                 //requests to api
          a = (JSON.parse(body))['rates'];                                //converts body to JSON
      for (i = 0; i < a.length; i++) {
        if (a[i].coin == coin) {
          price = parseFloat(price = a[i].rate).toFixed(2);           // assigns price
        }
      }
      quantity = (JSON.parse((JSON.parse(body))['volume']['value'])['INR'][coin.toUpperCase()]).toFixed(3);  //assigns qunatiity
      resolve();
    });
  });
  return [price, quantity, "Insta"];

}

function p2pSignal(coin, mode, noOfP2p, p2pList, message) {
  data = [];
  data.push(insta(mode, coin));                               // I want to store the data in data array
  console.log(data);
  //setTimeout(function(){console.log(data);},3000)
}

p2pSignal("XRP", "SELL", 4, 'INSTA', "1"); // CALLING THE FUNCTION

命令行代码:-

node cs2.js
[ Promise { <pending> } ]

1 个答案:

答案 0 :(得分:1)

使用async函数的函数应实现承诺控制流程,例如也要async

async function p2pSignal(coin, mode, noOfP2p, p2pList, message) {
  data = [];
  data.push(await insta(mode, coin));
  console.log(data);
}