I was wondering why functions that parses JSON files always use callback functions instead of just returning loaded response text? An example here and here. If it is possible, how would I return this value instead of callback'ing it?
closed as off-topic by Tim Holt, Jari Komppa, Noctrine♦ Apr 20 '14 at 15:00
|
|||||
|
The functions use AJAX in the form of the Because JavaScript runs in a single UI thread in modern browsers, making such a network call synchronously would lock the browser UI while it waits for the file request to complete, making it unresponsive and causing an extremely undesirable user experience. Thus the asynchronicity. Because the functions are asynchronous, you must have a callback function to handle the asynchronous response when it completes. You could immediately return a value from an asynchronous function, but that value could never be the result of the file request, since that would basically make the function synchronous again. |
|||||
|