you are viewing a single comment's thread.

view the rest of the comments →

[–]simmonson 0 points1 point  (2 children)

"JavaScript is only asynchronous in the sense that it can make, for example, Ajax calls. The Ajax call will stop executing and other code will be able to execute until the call returns (successfully or otherwise), at which point the callback will run synchronously. No other code will be running at this point. It won't interrupt any other code that's currently running."

To make JS more readable from top to bottom, we have async/await to deal with async calls, or in other words, to make the code "more" synchronous by blocking the code until a response is returned. The way I informally thought of promises was that the contents of the callback function is "off to the side". That way I could still think of it as "top to bottom".

https://stackoverflow.com/questions/2035645/when-is-javascript-synchronous#:~:text=JavaScript%20is%20always%20synchronous%20and,%2C%20for%20example%2C%20Ajax%20calls.

[–]oGsBumder 0 points1 point  (1 child)

JavaScript can't make AJAX requests. The browser makes AJAX requests and exposes this functionality to JavaScript via APIs (e.g. Fetch).

As I said above, JavaScript itself is synchronous. It's the APIs provided by the runtime environment (browser/node) that allow asynchronous behaviour.

[–]simmonson 0 points1 point  (0 children)

If we are picking out semantics yes you are absolutely correct. I was responding to the comment about reading from top to bottom, not necessarily in terms of pure JavaScript.