4 ms·
Async and parallel are a bit different. Parallel means that things execute at the same time (or at least appear to do so). Async means that you yield control t
by Yoric 2y ago
Async and parallel are a bit different.
Parallel means that things execute at the same time (or at least appear to do so). Async means that you yield control to some kind of scheduler and get it back at a later point.
Barring workers, JavaScript actually guarantees that two pieces of code never execute at the same time. That's the run-to-completion semantics.
When async was introduced to JavaScript from two different angles (callbacks in Node, Promise then await in browsers), there was limited parallelism involved (typically running I/O in the background), but the async was meant to ensure that:
1. while a task was (almost explicitly) waiting, another one could run;
2. in the case of the browser, we could yield control from JavaScript often enough that the UI could run smoothly.
- pavlov 2y agoNode didn’t introduce callbacks to JavaScript, they were present in the earliest browser APIs (img.onload, etc.)
- Yoric 2y agoFair enough, I meant CPS-style programming.
- layer8 2y agoThese were just regular events on the UI thread, not any different from onclick etc., IINM.
- pavlov 2y agoSo, the same thing as callbacks in Node.
- fenomas 2y agoWhen people talk about a callback in this kind of context they usually mean one function passed as an argument to another, in order to be invoked with the latter function's return value. Not event handlers like onclick, etc.
- layer8 2y agoI’m not familiar with Node, but static event handlers aren’t usually referred to as callbacks in that context. “Callback” implies that something is being called, for it to call back to the client code. That’s not what’s happening with the onxxx event handlers. There is no “back” there. The event loop simply calls “forward” into the JavaScript code.
- pavlov 2y agoI don’t know what you mean by “forward” because there is no difference between these two: img.onload = function() {…} img.src = “some url” img.load(“some url”, function() {…}) The early JavaScript APIs use the first style, but the result is the same.