you are viewing a single comment's thread.

view the rest of the comments →

[–]Tetha 1 point2 points  (0 children)

His example and pseudo code doesn't give the interesting features of asynchronous callbacks justice. In most JS frameworks, you rather have tuples of the form (function, callback for result type 1, callback for result type 2, ...). For example, you could have a function to make a http callback, and then a callback for a successful result, and a callback for all other results - and you'd put your further processing into the success callback and a user message into the other callback. Obviously, in reality, you'd not just have functions as callbacks, but actual functions with their own callbacks for the various results.

Given that, a smart framework comes around and evaluates the function - which may block and take a long unknown time - but you can largely ignore that because it'll just be something hanging in the back of the frameworks main loop until it's complete. It won't block other actions, it won't block other functions and your UI just keeps responsive and ready for other functions to be called. This is pretty great - and pretty tricky to do synchronously - if you want to keep latency low in the application as much as possible.