0

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

1
  • The ONLY place you can use balance is in your callback or in a function you call from the callback. That's how async code works in Javascript. Get used to it, design your code to work that way, stop looking for a way around it. Any code that needs balance should be in your callback or in a function that you call from the callback and you pass the balance to it. Commented Oct 11, 2017 at 13:54

1 Answer 1

1

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)
})
Sign up to request clarification or add additional context in comments.

2 Comments

Promises are great, but they aren't really relevant to the OP's question. You still end up with balance in a callback.
Yep, and that's the answer to the question. There's no way to do it. That's what the OP needs to understand. That's the real answer to their question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.