setimmediate

中文文档
1.0.5发布于 2016/09/12MITCommonJS零依赖近 30 天下载 2.8亿

A shim for the setImmediate efficient script yielding API

原始使用文档
文档目录10 节
  1. Introduction
  2. Macrotasks and Microtasks
  3. The Tricks
  4. process.nextTick
  5. postMessage
  6. MessageChannel
  7. <script> onreadystatechange
  8. Usage
  9. Demo
  10. Reference and Reading

setImmediate.js

A YuzuJS production

Introduction

setImmediate.js is a highly cross-browser implementation of the setImmediate and clearImmediate APIs, proposed by Microsoft to the Web Performance Working Group. setImmediate allows scripts to yield to the browser, executing a given operation asynchronously, in a manner that is typically more efficient and consumes less power than the usual setTimeout(..., 0) pattern.

setImmediate.js runs at “full speed” in the following browsers and environments, using various clever tricks:

  • Internet Explorer 6+
  • Firefox 3+
  • WebKit
  • Opera 9.5+
  • Node.js
  • Web workers in browsers that support MessageChannel, which I can't find solid info on.

In all other browsers we fall back to using setTimeout, so it's always safe to use.

Macrotasks and Microtasks

The setImmediate API, as specified, gives you access to the environment's task queue, sometimes known as its "macrotask" queue. This is crucially different from the microtask queue used by web features such as MutationObserver, language features such as promises and Object.observe, and Node.js features such as process.nextTick. Each go-around of the macrotask queue yields back to the event loop once all queued tasks have been processed, even if the macrotask itself queued more macrotasks. Whereas, the microtask queue will continue executing any queued microtasks until it is exhausted.

In practice, what this means is that if you call setImmediate inside of another task queued with setImmediate, you will yield back to the event loop and any I/O or rendering tasks that need to take place between those calls, instead of executing the queued task as soon as possible.

If you are looking specifically to yield as part of a render loop, consider using requestAnimationFrame; if you are looking solely for the control-flow ordering effects, use a microtask solution such as asap.

The Tricks

process.nextTick

In Node.js versions below 0.9, setImmediate is not available, but process.nextTick is—and in those versions, process.nextTick uses macrotask semantics. So, we use it to shim support for a global setImmediate.

In Node.js 0.9 and above, process.nextTick moved to microtask semantics, but setImmediate was introduced with macrotask semantics, so there's no need to polyfill anything.

Note that we check for actual Node.js environments, not emulated ones like those produced by browserify or similar. Such emulated environments often already include a process.nextTick shim that's not as browser-compatible as setImmediate.js.

postMessage

In Firefox 3+, Internet Explorer 9+, all modern WebKit browsers, and Opera 9.5+, postMessage is available and provides a good way to queue tasks on the event loop. It's quite the abuse, using a cross-document messaging protocol within the same document simply to get access to the event loop task queue, but until there are native implementations, this is the best option.

Note that Internet Explorer 8 includes a synchronous version of postMessage. We detect this, or any other such synchronous implementation, and fall back to another trick.

MessageChannel

Unfortunately, postMessage has completely different semantics inside web workers, and so cannot be used there. So we turn to MessageChannel, which has worse browser support, but does work inside a web worker.

<script> onreadystatechange

For our last trick, we pull something out to make things fast in Internet Explorer versions 6 through 8: namely, creating a <script> element and firing our calls in its onreadystatechange event. This does execute in a future turn of the event loop, and is also faster than setTimeout(…, 0), so hey, why not?

Usage

In the browser, include it with a <script> tag; pretty simple.

In Node.js, do

npm install --save setimmediate

then

require("setimmediate");  // (somewhere early in your app; it attaches to the global scope.)

Demo

Reference and Reading

版本与发布记录

setimmediate 共发布 6 个版本,首次发布于 2012/12/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.5latest2016/09/1210 年前
1.0.42015/10/1811 年前
1.0.32015/10/1711 年前
1.0.22014/04/2812 年前
1.0.12013/01/0713 年前
1.0.02012/12/3113 年前

关于 setimmediate 的常见问题

setimmediate 是什么?

A shim for the setImmediate efficient script yielding API

如何安装 setimmediate?

在项目目录运行 npm install setimmediate,或使用 pnpm add / yarn add / bun add。

setimmediate 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/setimmediate 是否存在。

setimmediate 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

setimmediate 可以商用吗?

setimmediate 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

setimmediate 的下载量有多少?

根据 npm 官方下载统计,setimmediate 在 2026/10/07 – 2026/11/05 这 30 天内被下载 280,755,622 次,日均约 9,358,521 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

setimmediate 还在维护吗?

最新版本 1.0.5 发布于 2016/09/12(10 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2012/12/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。