Nodejs Event Loop

Source: Internet
Author: User

1. With only one main thread, node starts executing the script with advanced event loop initialization (synchronous task, issue asynchronous request, plan timer effective time, execute promise.nexttick, etc.), this is the event loop has not started.

2. Nodejs the six stages of each round of event loops (the event loop executes indefinitely until the callback function queue of the asynchronous task is emptied to stop execution):

  1. Timers (callback function for handling settimeout and SetInterval)
  2. I/O callbacks (except settimeout, SetInterval, Setimmediate, callback function for closing the request)
  3. Idle,prepare (LIBUV internal use)
  4. Poll (Waiting for I/O events that have not yet returned)
  5. Check (setimmediate)
  6. Close callbacks (the callback that performs the shutdown request, such as Socket.on (' close ', ...) )

3. settimeout and Setimmediate:

Since the second parameter of settimeout is 0 by default, but with node not being true 0ms, 1s is required at least, so if the actual execution goes into the event loop, if it is not 1ms, then the timers stage will skip to the check stage, so the order of execution is indeterminate.

4. There are two types of asynchronous tasks:

This round loop: Promise.nexttick, Promise callback function

Second wheel cycle: SetTimeout, Setinteval, Setimmediate callback function

Nodejs Event Loop

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.