Smitk
Smitk

Reputation: 91

Promise received as pending

This code returns a promise which is pending I want to store the returned data from insta function into data array.

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

Command Line Code:-

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

Upvotes: 1

Views: 70

Answers (1)

Estus Flask
Estus Flask

Reputation: 222369

A function that uses async function should implement promise control flow, e.g. be async too:

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

Upvotes: 1

Related Questions