1

I don't like to ask a question but I feel I need to know how, or a better way of doing this. I should say first that I'm new to Javascript having done Java before and am new to callbacks as well. I read some other questions, but am still having some difficulty. This is truly horrible.

In rounds.js I have this

 exports.getCurrentRound = function(callback) {

      exec(header + token + address, function (error, stdout, stderr) {
           sys.print('stdout: ' + stdout);
           rounds = stdout;

           if (error !== null)
               callback(null, err);
           else
               callback(null, currentRound);
     });
}

In index.js I have this.

var result;

    rounds.getCurrentRound( function (err, cr) {

        currentRound = cr;
        console.log("the current round is" + currentRound);

    });

How can i get result to equal something? Hope that makes sense. Keep in mind i'm running windows. And am reluctant to use promises. Whatever that is. I hope I'm not wasting your time too much.

Thanks.

Alternatively if there's a way to just run this synchronously please tell me what it is. I just want to know the proper way of doing this.

S Donnelly
  • 49
  • 4

1 Answers1

0

You're nearly there! You've defined result at the top but you now just need to set it.

var result; // default: undefined
rounds.getCurrentRound( function (err, cr) {
  currentRound = cr;
  console.log("the current round is" + currentRound);
  result = currentRound;
});
// result will be set at some point in the future

The problem is that you don't know when result will be set. You can find out by constantly checking if it's set using setTimeout, but even that is asynchronous!

The answer is you need to treat your whole program asynchronously. Whoever needs result will have to pass in a callback.

Promises are just a way to say 'I want the value of this variable whenever it is set'.

var result = new Promise(function(resolve, reject){
  rounds.getCurrentRound( function (err, cr) {
    currentRound = cr;
    console.log("the current round is" + currentRound);
    resolve(currentRound); // set the actual value of result
  });
});

// you can use the value of result later like this:
result.then(function(resultValue){
  console.log('The result is ' + resultValue);
});

But notice that even then, to use the value of result you still need to pass a callback. It's still asynchronous! But the benefit you gain is that you can pass the result promise around your code and other bits of code can use the resolved promise to perform something once the value is set. For instance, let's say you want to use the value of result in two places in your code...

result.then(function(resultValue){
  console.log('My code is using result ' + resultValue);
});

// somewhere else
result.then(function(resultValue){
  console.log('This other code is also using result ' + resultValue);
});

If you're using the callback method that you first started out with (in your question), both cases where you need to use the value of result need to be inside the callback. But using promises, we've managed to separate them out.

In short, the answer is that you have to think about your code slightly differently, instead of thinking about it as a series of steps, try thinking about it as a series of events that might happen in sequence but might also happen 'whenever they're done' - asynchronously.

meltuhamy
  • 3,293
  • 4
  • 23
  • 22