0

I want to change an array of strings in a callback function and also allow it to be accessible outside of the callback. At line A console prints object. At line B however, console prints undefined, and thus I cannot access the information that was stored in the callback. What is going on here and how can I fix this?

var A;
tempService.route(
  function() {
  A = [2];
  A[0] = "Bob";
  A[1] = "Joe";
  console.log(typeof A); //line A
})
console.log(typeof A);  //line B
varimax
  • 111
  • 1
  • 13
  • "Jim, I need the source of that quote. Write it down on this paper when you find it. Jack, here's the name of the guy who said the quote, put it into the article. Why is this article still not attributed? I thought I told Jim to write it down on this paper!" - cue 15 minutes later, Jim looking for that piece of paper to write down the name he just found. "Was stored in the callback"? No, *will be* stored in the callback. Only ever access results from async inside the callback (or functions called from the callback). – Amadan Jan 28 '16 at 05:09

1 Answers1

0

At line B, your callback has not YET been called so A still has its original value. It will be called sometime in the future after Line B runs.

jfriend00
  • 683,504
  • 96
  • 985
  • 979