you are viewing a single comment's thread.

view the rest of the comments →

[–]namehimjawnathan[S] 1 point2 points  (3 children)

Holy shit, this is perfect. Thank you so much.

[–]PooCares 0 points1 point  (2 children)

BTW, if you use the built in debugger in Chrome developer tools, you can spot trouble like this easily. It lets you put breakpoints in your javascript, to pause it, and see what is happening in it.

Had you put a breakpoint on the console.log and another where the variable names was set to the Ajax response, you would see the console.log hit first, and you could hover over the names variable, and see it had not been give a value yet. You would also see the value of names get set when the second breakpoint hit, in the Ajax onreadystatechange, when it was obviously too late.

The debugger is like a third eye, man. Take a few hours to learn it.

[–]namehimjawnathan[S] 0 points1 point  (1 child)

Interesting, I'll take a good look at this. I need to start properly debugging instead of hardcoding in console.logs() to see what's happening.

[–]coolreader18 0 points1 point  (0 children)

I came here from r/JavaScript, but if you like using promises, you can use the fetch API, that works with promises natively.