0

firstly i don't care about code effeicney and bets practise at the moment, only how variables work with arrow operators can some one explain why when one uses '=>' arrow operator within the code block it doesn't save or add to a variable already delate outside? I've tried 'var ttest;', 'var ttest={};', etc... how can I use something which is calculated or retrieved after the '=> {' on a global scale?

essentially what I need to do is to be able to add'{'Balance': balance,'price': price,'value': gbpvalue,'btcvalue': btcavlue};', to a global object and/or array or just use that info somehow out side:

'then(accounts => {...});'

let ttest;
client.rest.account.listAccounts().then(accounts => {
    //console.log(accounts);
    for (var item in accounts){
        if (accounts[item]['currency'] == 'ETH'){
            var balance = parseFloat(accounts[item]['balance']) + parseFloat(accounts[item]['hold']);
            var price = parseFloat(apipriceReq('ETHGBP').price);
            var gbpvalue = price * balance;
            var btcavlue = (gbpvalue/(btcp / 100)/100);
            ttest = {'Balance': balance,'price': price,'value': gbpvalue,'btcvalue': btcavlue};
            }
        }
    });
//var ttt = client.rest.account.listAccounts();
console.log(ttest)
TGK-UK
  • 49
  • 4
  • 2
    This behaviour has nothing to do with arrow functions, but with the asynchronous nature of a `then` callback. That callback is called *later*. And you cannot hope to log *now* what will only be available in some *future*. – trincot Mar 28 '21 at 13:37
  • actually that is very useful, thank you sir, so i've also tried, 'var ttt = client.rest.account.listAccounts(); console.log(ttt)' and i get : 'Promise { }', again it hasn't got the reply yet using 'var ttt = await client.rest.account.listAccounts();' i get SyntaxError: await is only valid in async function, i'm a bit buggerd, how the hell do I extract the eventual return of client.rest.account.listAccounts(), or force to run in sync not async, apologies still a JavaScript noob – TGK-UK Mar 28 '21 at 14:36
  • Whatever you do, you'll not be able to *return* the value synchronously. You should embrace the asynchronous pattern. Read the information in the Q&A I linked to in the blue banner at the top. – trincot Mar 28 '21 at 14:42
  • apricated yes that looks very interesting – TGK-UK Mar 28 '21 at 14:53

0 Answers0