김락근
김락근

Reputation: 41

Node.js - How to get callback function's variable out of function?

balance.js

var CoinStack = require('coinstack-sdk-js')
var accessKey = 'c7dbfacbdf1510889b38c01b8440b1';
var secretKey = '10e88e9904f29c98356fd2d12b26de';
var client = new CoinStack(accessKey, secretKey);

client.getBalance(address, function(err, balance) {
    console.log('balance', balance)
});

I have a this code, then i want to get var balance out of function

i don't know what to do, i want fixed code

i need your helps

Upvotes: 0

Views: 50

Answers (1)

El houcine bougarfaoui
El houcine bougarfaoui

Reputation: 37403

You can not, consider using Promises like this :

var CoinStack = require('coinstack-sdk-js')
var accessKey = 'c7dbfacbdf1510889b38c01b8440b1';
var secretKey = '10e88e9904f29c98356fd2d12b26de';
var client = new CoinStack(accessKey, secretKey);
let balancePromise = new Promise((resolve, reject)=>{

   client.getBalance(address, function(err, balance) {
     if(err) 
        reject(err)
     else 
       resolve(balance);
   });
})

// how to get balance value
balancePromise.then((balance)=>{
  console.log('balance', balance)
}).catch((error)=>{
  console.log('error', error)
})

Upvotes: 1

Related Questions