Search code examples
node.jsasynchronouspromisereturntron

Nodejs return result from function is empty promise


I am trying to use Nodejs tron library to create pair of wallet address and here is my code:

app.js

var app = require('express')()
var http = require('http').createServer(app)
var wallet_engine = require('./function')
app.get('/', function (req, res) {
    result = wallet_engine.create_wallet_trx()
    console.log(result)
    res.send(result)
})
//////server listen
http.listen(8443, function () {
    console.log('listening on *:8443')
})

and here is my function.js

module.exports = {
    create_wallet_trx: function () {
////////generate TRX Address
        var { HdTronPayments } = require('@faast/tron-payments')
        var keys = HdTronPayments.generateNewKeys()
        var tronPayments = new HdTronPayments({ hdKey: keys.xprv })
        var address = tronPayments.getPayport(356)
        var privateKey = tronPayments.getPrivateKey(356)
        var trx_wallet = { privateKey: privateKey, address: address }
        console.log(trx_wallet)
        return trx_wallet
    },
}

The problem is when i check console.log(trx_wallet) the result is there and i can see generated public and private key, also after returning data, console.log(result) is displaying data, but res.send(result) shows me empty json.

this is console.log() results

{
  privateKey: Promise {
    'B88BB56DAB80DB681765A0C07197DD23BB8E2FAD195BF9D0ECD09F5F8FC54297'
  },
  address: Promise { { address: 'TYCJSKERHReUXacw9wLorZYLDoijevvsVK' } }
}

and this is the result on my browser:

{"privateKey":{},"address":{}}

i know this is because of Nodejs Async system and i should wait to promise gets the value but i don't know how to wait for promise to get complete and then prints the result on my browser screen.


Solution

  • You are doing good but here many calls are asynchronous that's for you facing problem. you should use async await or then as i did bellow it may help you..

    // app js
    
    
    var app = require('express')()
    var http = require('http').createServer(app)
    var wallet_engine = require('./function')
    app.get('/', function (req, res) {
        result = wallet_engine.create_wallet_trx().then(data=>{
          res.json(data);
        }).catch(err=>{
          console.log(err);
        })
    })
    //////server listen
    http.listen(8443, function () {
        console.log('listening on *:8443')
    })
    
    
    
    // function.js
    
    var { HdTronPayments } = require('@faast/tron-payments')
    
    module.exports = {
      create_wallet_trx: async function () {
          var keys = await HdTronPayments.generateNewKeys()
          var tronPayments = await new HdTronPayments({ hdKey: keys.xprv })
          var address = await tronPayments.getPayport(356)
          var privateKey = await tronPayments.getPrivateKey(356)
          var trx_wallet = { privateKey: privateKey, address: address }
          return trx_wallet
      }
    }

    result: {"privateKey":"92ACAECFE00E9F90E330A6B031F10365F29AFDD503922CC99CA8704F1BA53432","address":{"address":"TGfsHx4VU6B36AwUy8Bqt6edoNnUHpMtSQ"}}