Hello, so I was re-watching Philip Roberts' explanation on the event loop yesterday: https://www.youtube.com/watch?v=8aGhZQkoFbQand I just had some questions to clarify things since he mostly focused on the Browser APIs.The NodeJS' runtime along with the Browsers' runtime are practically similar, the Browser has its own Web APIs, one of them being setTimeout, allowing callbacks to be queued and once the call stack is empty, those callbacks are then pushed onto the stack and de-queued from the queue, right?And since NodeJS itself has its own C++ APIs, those APIs take care of the same features practically the same way but instead we're not dealing with browser APIs anymore. Instead we have a thread pool that'll handle the scheduling of callbacks, pushing them onto the event queue, and then NodeJS' event loop will handle callbacks pushed onto the stack?So although JavaScript itself is single threaded, the other APIs run on their own threads. So when you're handling an asynchronous function, let's say fs.readdir('/path/to/file', cb);readdir will attempt to read the directory, so this function call is passed onto the thread pool, the thread pool will take care of when the callback should be invoked by pushing it into the event queue, and likewise with the browser, it'll call the callback when the stack is empty?
Submitted March 23, 2020 at 07:24PM by ansonplusc
No comments:
Post a Comment