immediate

中文文档
3.3.0发布于 2020/06/09MITCommonJS零依赖近 30 天下载 2.3亿

A cross browser microtask library

原始使用文档
文档目录10 节
  1. Introduction
  2. The Tricks
  3. process.nextTick
  4. queueMicrotask
  5. MutationObserver
  6. MessageChannel
  7. <script> onreadystatechange
  8. Tricks we don't use
  9. setImmediate
  10. Reference and Reading

immediate Build Status

testling status

npm install immediate --save

then

var immediate = require("immediate");

immediate(function () {
  // this will run soon
});

immediate(function (arg1, arg2) {
  // get your args like in iojs
}, thing1, thing2);

Introduction

immediate is a microtask library, descended from NobleJS's setImmediate, but including ideas from Cujo's When and RSVP.

immediate takes the tricks from setImmediate and RSVP and combines them with the scheduler inspired (vaguely) by when's.

Note versions 2.6.5 and earlier were strictly speaking a 'macrotask' library not a microtask one, see this for the difference, if you need a macrotask library, I got you covered.

Several new features were added in versions 3.1.0 and 3.2.0 to maintain parity with process.nextTick, but the 3.0.x series is still being kept up to date if you just need the small barebones version

The Tricks

process.nextTick

Note that we check for actual Node.js environments, not emulated ones like those produced by browserify or similar.

queueMicrotask

Function available in major browser these days which you can use to add a function into the microtask queue managed by V8.

MutationObserver

This is what RSVP uses, it's very fast, details on MDN.

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?

Tricks we don't use

setImmediate

We avoid using setImmediate because node's process.nextTick is better suited to our needs. Additionally, Internet Explorer 10's implementation of setImmediate is broken.

Reference and Reading

版本与发布记录

immediate 共发布 31 个版本,首次发布于 2013/09/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.3.0latest2020/06/096 年前
3.2.32016/08/1810 年前
3.0.62016/07/2810 年前
3.2.22015/10/1211 年前
3.0.5lie2015/08/0711 年前
3.2.12015/05/2711 年前

关于 immediate 的常见问题

immediate 是什么?

A cross browser microtask library

如何安装 immediate?

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

immediate 支持 TypeScript 吗?

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

immediate 支持 ESM 还是 CommonJS?

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

immediate 可以商用吗?

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

immediate 的下载量有多少?

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

immediate 还在维护吗?

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