How does jQuery have asynchronous functions?
- by Sam.Rueby
I'm surprised I can't find a clear answer to this. So, in jQuery, you can do this:
$(someElements).fadeOut(1000);
$(someElements).remove();
Which, will start a fadeOut animation, but before it finishes executing in the 1 second duration, the elements are removed from the DOM. But how is this possible? I keep reading the JavaScript is single threaded. ( Is javascript guaranteed to be single-threaded? ) This question is not "How do I fix this?" I know I can do either: $(someElements).fadeOut(1000).promise().done(function() { $(someElements).remove();});, or even better:$(someElements).fadeOut(1000, function() { $(this).remove(); } );
What I don't understand is how JavaScript runs in a "single thread" but I'm able to use these jQuery functions that execute asynchronously and visibly see the DOM change in different places at the same time. How does it work?