标签: 事件循环
8 个内容
笔记(7)
本文解析了JavaScript中`var`与`let`在循环闭包中的差异,揭示了变量提升、块级作用域和词法环境等核心概念。对比了历史解决方案、现代工程实践以及其他语言的实现,强调使用`let`构建清晰作用域边界的重要性,以避免常见陷阱并提升代码可预测性。
本文深入解析 JavaScript Promise,阐述其作为有限状态机的核心机制、微任务执行原理和链式调用。对比 `all`、`allSettled` 和 `any` 等静态方法,探讨取消模式、内存泄漏规避及性能优化。最后提及 Promise 实现规范和未来演进,强调实践中的最佳策略。
该文探讨Node.js同步/异步文件读取性能差异及阻塞问题。背景:`fs.promises.readFile`比`fs.readFile`慢。问题:同步读取阻塞事件循环,影响并发。方案:避免同步I/O,采用异步方式,如`fs.promises.readFile`,提升服务器并发性能。
本文深入解析了 Promise 的三种状态(pending, fulfilled, rejected)、resolve参数的不同类型处理、then/catch/finally方法,以及静态方法 all, allSettled, race, any 的用法。最后,文章还提供了一个 Promise 的简单实现,并简要提及了 postMessage 相关的异步任务执行顺序问题。
动态(1)
The queueMicrotask() method, which is exposed on the Window or Worker interface, queues a microtask to be executed at a safe time prior to control returning to the browser's event loop.
First, nextTick queue is checked to get tasks for its execution, once it is exhausted, the microTasks queue is checked. After finishing the tasks in the microtask queue the process of checking nextTick and microTasks queues are repeated until the queues have been emptied.
They have different queues. The nextTick's queue is managed by node and the microtask one is managed by v8.
The nextTick queue is checked first after the current function/script execution, and then the microTask one.