0

I'm trying to use the value of a global variable outside of a function which updates it. But, processing this way, I dont't get the expected result as the below code explains it.

var res = {};
client.fetchOne(query, function(err, response) {
    res = response;
    console.log(res);
}); //the expected result is displayed here
console.log(res); //{} is displayed here

What's wrong with this? Do I need to use closures? Any brilliant idea, please?

John Aims
  • 49
  • 7
  • What you need to do is wrap it in an async function and place the keyword `await` before your call to `fetchOne`. –  Jun 18 '15 at 09:24
  • 1
    @torazaburo: `async/await` is a proposal for ES7. If you are going to suggest something like this, you have to provide more information, otherwise your comment confuses more than it helps. `await` also only works on functions that return a promise, and it doesn't look like `client.fetchOne` does. – Felix Kling Jun 18 '15 at 09:26
  • You're right, but it was an off-the-cuff comment on a closed question, thought it might stimulate the OP to go out and check out async functions, at which point he would learn all the stuff you correctly mention. –  Jun 18 '15 at 09:29

0 Answers0