Company
Date Published
Author
Michael Gokhman
Word count
3451
Language
English
Hacker News points
3

Summary

The Node.js Event-Loop can be blocked by async functions, even with the use of async/await keywords. This happens because the event-loop only checks for pending callbacks in specific phases (timers, pending callbacks, idle, prepare, poll, check, and close). If a callback enqueues another callback that can be handled in the same phase, it will be handled before moving to the next phase. However, if an async function uses promises or timers, it may block the event-loop if not used correctly. The use of `setImmediate()` or `setTimeout()` with a non-zero delay can help partition long-running synchronous code and prevent blocking the event-loop. Nevertheless, there is still an overhead in using these functions, and finding the right balance between blocking and yielding is crucial to avoid performance issues. Additionally, some built-in JavaScript functions like `JSON.parse()` can block the event-loop as well. Understanding the Node.js Event-Loop and its behavior is essential for writing efficient and non-blocking code.