Reputation: 156
I need to understand these concepts more clearly as i'm not sure about them. Javascript is single-threaded, so far, so good.
From my understanding setTimeout is just delaying the execution of the code from the main thread but the callback it's still executed on the main thread when the call stack is empty. Is this correct? Same would be true for Promises and Event Handlers.
setTimeout(() => {
console.log('this line of code is executed on the main thread');
}, 5000);
For asynchronous requests using XMLHttpRequest the actual request is sent to a separate API implemented by the browser that runs it on a separate thread, thus, it is truly asynchronous. Is this correct?
WebWorkers would be another API implemented by the browser that also runs the code from the given script in a background thread. Is this correct?
I would greatly appreciate an answer to this question!
Upvotes: 2
Views: 250
Reputation: 53173
You're generally correct in all three points.
Upvotes: 1