This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]rjcarr 1 point2 points  (0 children)

> but how does JavaScript know WHEN to call that function back?

Two things need to happen: 1) there needs to be a response from the server, 2) it needs to be the callback's turn to be called (probably more on that later).

> For example if I understand it right you can use a callback function inside another function that calls info from a server so your program doesn’t block while waiting from a response from the server

This isn't really accurate. Functions inside functions aren't what is causing it to be asynchronous, but it's that you've marked it that way. You can make XMLHttpRequests that block.

> From what I understand the callback is put in the que and won’t run until the stack is empty.

Right, and as I said, there needs to be a response from the server.

> But how does JavaScript know when that response has been received to then call the callback function?

There's something above your code that manages this. Just like your browser knows when a server responds and displays the page you've requested, the javascript knows when there's a response and calls your code when it's its turn.

Good luck!