Friday, 28 April 2017

Node.JS callbacks - how to use the variables outside the callback [duplicate]

This question already has an answer here:

I've been trying to understand callbacks, but I just couldn't successfully execute that code :

function getAccountIDs(currency, callback) {
  client.getAccount(currency, function(err, account) {
    callback(null, account.id);
  });
}


function sellCurrency(currencyTake, currencyPut) {

  getAccountIDs(currencyTake, function(err, id) {

    console.log(id); // this does work.

  });

  console.log(id); // this doesn't work
}

How could I fix the second console.log(id); part that doesn't work. I would just like to use my variable - id outside the callback.



via Luka Kovačič

No comments:

Post a Comment