So the way I understand the word asynchronous is that while some task runs in the background another code can be executed. But since JS is single threaded how do callbacks etc. make it asynchronous? Promises are basically: When this is finished, then do this, then do this, and if there is an error I'm going to catch it. How does that match up with the definition? There is no code that runs simultaneously. Promises are basically if-statements. I'm new so I am sorry if I ask stupid questions.
Asked
Active
Viewed 50 times
1
-
"Asynchronous" doesn't mean "Multi-Threaded." It means "Free up the underlying thread for other tasks while we wait on this." An example: waiting for a JSON request to complete. It doesn't have to be another thread. – Robert Harvey Jul 28 '20 at 21:00
-
"*how do callbacks make it asynchronous?*" - callbacks don't *make* anything asynchronous at all. Callbacks are just a tool to expose some underlying asynchronous functionality. You say "When this network packet was received, as soon as the thread is idle, then run this function with the network data as an argument". When you made that statement about the future, you can immediately start doing other things. It's a *when*, not an *if* that waits for the condition to occur. – Bergi Jul 28 '20 at 23:17