How does the callback function that I set go into the event queue?

Asked 2 years ago, Updated 2 years ago, 37 views

I'm studying the event loop of NodeJS. I looked it up on the Internet, read nodejs, libuv source code,

setTimeout( function(){ console.log("Hello World!") }, 1000 );

Here, I don't know how that setTimeout function puts function(){ console.log("HelloWorld")} in the timer queue. Help!

node.js

2022-09-21 19:51

1 Answers

Turn the timer on another thread and message the node when the set time expires. The node is the subject of creating the thread and ordering it to be informed when it is complete, but creating and managing the actual thread depends on the OS kernel implementation, so we delegate tasks that need to operate asynchronously. I think it's through the libuv library you mentioned.

At some point, the asynchronous operation will complete, and the kernel sends a completion signal to the node. At this time, you enter the queue, and the loop is polling, and when the call stack is empty, you execute the callbacks accumulated in the queue sequentially.

The part of sending and receiving messages and threading processing will not differ from OS to OS where the node is installed, but the detailed implementation will differ slightly from kernel to kernel for each OS.

https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/


2022-09-21 19:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.