How does a single thread handle asynchronous code in JavaScript?

Source: Internet
Author: User

Original: Https://www.quora.com/How-does-a-single-thread-handle-asynchronous-code-in-JavaScript

--------------------------------------------------------------------------------

Well, arguably it isn't true that Javascript is a threaded if you see from the under Hood working of browser JS to you R JS Code, there is thread pools. By single threaded what they mean (browser end) is your JS runs to a single threaded event loop. There is a single thread that handles your event loop. Under your JS, the browser code is running multiple threads to capture events and trigger handlers while they capture any New event, they push it on a event queue and then this event loop, in which your code is running gets triggered and it ha Ndles the request e.g. it performs an action which can is to show a DIV, which again triggers the Browser to print It, whi Ch in turn runs a thread to does it (from the thread pool).

Lets take an example of your JS algo.
window.onload
Show Header
Send a Ajax Req. For config-> when done, alert a box
Do Some more page animations
told the browser to send a AJAX request with a provided callback, it saves it in memory and this Network IO C All are transferred to a thread in the Network Pool Threads, your code next to this line would continue to work. After the "Network call" thread has the do its job, it would push on the event queue the response. Remember that event loop? Here's when it comes to action, it picks the top entity and then trigger your callback (Context switching on CPU level), W Hich in turn could is anything. Now try doing something like this.
window.onload
Show Header
Send a Ajax req. for config
When done, Trigger another Ajax
For Loop 0.100000000
Do more animation

Now here, if the even your second Ajax completes in due time, its callback would is not being triggered until your for loop exits. Its a sort of blocking within the thread, none of the event would be fired and everything would be frozen!

Hope that clears your doubt.

cheers!

How does a single thread handle asynchronous code in JavaScript?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.