While the question may seem to have been asked multiple times and already have highly rated answers to me, I would like to suggest that multiple answers are conflicting and I can never come to a complete understanding of the internals of asynchronous code. I fully understand that it means to continue sequential code execution and complete the task later, I am trying to understand that later portion.
Answer 1 - Suggests UI/Main
Firstly, this question contained the following test inside of it, suggesting that asyncrhonous code runs on the Main/Ui thread and links an article explaining why there is no other thread for asynchronous code.
Q: "In my mind, since I do mainly UI dev, async code is code that does not run on the UI thread, but on some other thread. "
A: That belief is common but false. There is no requirement that asynchronous code run on any second thread.
Infact the answer suggests that asynchronous programming with threads is "wrong" by saying, "Threads are workers. Asynchronous workflows can happen all on one thread. The point of the asynchronous workflow is to avoid hiring more workers if you can possibly avoid it."
Answer 2 - Suggests UI/Main
The next question I have read also suggests that Asynchronous code is ran on the Main/UI thread, however the comparison used is JavaScript, which we all know is a single-threaded language. For example, suppose I run this code,
function wait(ms) {
var start = Date.now(),
now = start;
while (now - start < ms) {
now = Date.now();
}
}
setTimeout(() => {
wait(5000);
}, 3000)
setTimeout
will be called Asynchronously and after 3000ms
the callback will be added to the Event Loop
and eventually ran. However the method execution will be done on the Main/UI thread and thus, resulting in the UI being frozen for 5000ms
.
Answer 3 - Suggesting new thread
This answer suggests that Asynchronous code is ran into a new thread, inside the answer he says. "When you execute something asynchronously, you can move on to another task before it finishes. That being said, in the context of computers this translates into executing a process or task on another "thread."
Answer 4 - Suggesting new thread
The final answer suggests that Asynchronous code is thread based by saying, "In the general case, an asynchronous call does not necessarily create a new thread. That's one way to implement it, with a pre-existing thread pool or external process being other ways. It depends heavily on language, object model (if any), and run time environment. Asynchronous just means the calling thread doesn't sit and wait for the response, nor does the asynchronous activity happen in the calling thread.", with the main point being "nor does the asynchronous activity happen in the calling thread"
I am not sure where to go from this point, from my understanding I would think Asynchronous code must execute in a different thread, to never block the UI, with the only exception being JavaScript, I suppose. However, even when it comes to the execution of Asynchronous code on a single threaded language, I would have thought that any function callback that is ran, must be ran to completion before another callback is ran.
The second answer on here suggests by drawing, when Asynchronous code is ran on a single thread, it stops and runs a different Asynchronous callback, switching back and forth, much like a thread would until they're finished. This to me doesn't make complete sense since with Asynchronous code, they generally have a callback included to prevent any sort of race condition, suppose B required A to run to completion first, B would be a callback inside of A. The callbacks A and B would not "run together" and switch between each other.