原文:
The JavaScript event loop is the mechanism that enables asynchronous programming in a single-threaded environment. When asynchronous operations like setTimeout or fetch complete, their callbacks are placed in a queue. The event loop continuously checks whether the call stack is empty; if so, it pushes the next callback onto the stack. Microtasks, including Promise callbacks, have higher priority than macrotasks. This model explains why setTimeout(fn, 0) doesn't execute immediately — it waits for the current stack to clear and all microtasks to finish.
翻译:
JavaScript事件循环是在单线程环境中实现异步编程的机制。当setTimeout或fetch等异步操作完成时,它们的回调被放入队列中。事件循环持续检查调用栈是否为空;如果为空,它将下一个回调推入栈中。微任务(包括Promise回调)的优先级高于宏任务。这个模型解释了为什么setTimeout(fn, 0)不会立即执行——它等待当前栈清空并且所有微任务完成。
词汇注释:
