这些库是在 ES6 原生 Promise 标准化之前出现的异步解决方案,旨在解决回调地狱问题并提供更清晰的异步流程控制。bluebird 以功能丰富著称,提供取消、超时等高级特性;lie 是一个轻量的 Promise polyfill,主要用于旧浏览器兼容;q、rsvp 和 when 则是早期流行的 Promise 实现,如今大多已进入维护或弃用状态。在现代开发中,原生 Promise 通常是首选,但在维护旧项目或特定场景下,了解这些库的差异仍有必要。
在 ES6 原生 Promise 普及之前,JavaScript 社区涌现了多个 Promise 实现库,用于解决异步回调嵌套问题。bluebird、lie、q、rsvp 和 when 是其中的代表。虽然现代开发已转向原生 Promise,但理解它们的差异有助于维护旧项目或做出架构决策。本文将深入对比它们的核心用法、错误处理及维护状态。
所有库都支持链式调用,但初始化方式和 API 细节有所不同。原生 Promise 标准确立后,这些库大多向标准靠拢,但早期设计仍有残留。
bluebird 完全兼容原生 Promise 构造函数,并扩展了静态方法。
// bluebird: 兼容原生构造函数
const Promise = require('bluebird');
new Promise((resolve, reject) => {
resolve('done');
}).then(val => console.log(val));
lie 严格遵循 Promise/A+ 规范,作为 polyfill 使用时 API 与原生一致。
// lie: 作为 polyfill 使用
var Promise = require('lie');
new Promise((resolve, reject) => {
resolve('done');
}).then(val => console.log(val));
q 早期版本常用 deferred 对象,后期支持构造函数,但社区代码中仍多见 deferred 模式。
// q: 常用 deferred 模式
var Q = require('q');
var deferred = Q.defer();
deferred.resolve('done');
deferred.promise.then(val => console.log(val));
rsvp 提供构造函数,曾是 Ember 的默认实现,API 接近原生。
// rsvp: 构造函数模式
var RSVP = require('rsvp');
new RSVP.Promise((resolve, reject) => {
resolve('done');
}).then(val => console.log(val));
when 提供 when.promise 构造函数,也支持 when() 包装值。
// when: 构造函数或包装
var when = require('when');
when.promise((resolve, reject) => {
resolve('done');
}).then(val => console.log(val));
错误处理是 Promise 库的核心差异点。原生 Promise 使用 .catch 和 .finally,旧库可能有不同的命名或行为。
bluebird 支持 .catch 和 .finally,并允许过滤特定错误类型。
// bluebird: 支持错误过滤
Promise.reject(new TypeError('bad'))
.catch(TypeError, e => console.log('Type Error', e))
.finally(() => console.log('clean up'));
lie 遵循标准,使用 .catch 和 .finally(需 polyfill 支持)。
// lie: 标准错误处理
Promise.reject('error')
.catch(e => console.log('caught', e))
.finally(() => console.log('done'));
q 早期使用 .fail 和 .fin,后期兼容 .catch 但文档仍常见旧写法。
// q: 旧式错误处理
Q.reject('error')
.fail(e => console.log('caught', e))
.fin(() => console.log('done'));
rsvp 支持 .catch 和 .finally,与原生行为一致。
// rsvp: 标准错误处理
RSVP.Promise.reject('error')
.catch(e => console.log('caught', e))
.finally(() => console.log('done'));
when 使用 .otherwise 和 .ensure,命名与原生不同。
// when: 特有命名
when.reject('error')
.otherwise(e => console.log('caught', e))
.ensure(() => console.log('done'));
这是 bluebird 与其他库最大的区别所在。原生 Promise 早期缺乏并发控制方法,bluebird 填补了这一空白。
bluebird 提供 Promise.map、Promise.timeout 和取消功能。
// bluebird: 并发映射与超时
Promise.map([1, 2, 3], item => fetch(item), { concurrency: 2 })
.timeout(5000)
.then(results => console.log(results));
lie 无高级功能,仅实现基础 Promise 规范。
// lie: 无高级并发方法
// 需手动实现 Promise.all
Promise.all([p1, p2]).then(results => console.log(results));
q 提供 Q.all、Q.spread,但无原生取消支持。
// q: 基础并发
Q.all([p1, p2]).spread((res1, res2) => console.log(res1, res2));
rsvp 提供 RSVP.all、RSVP.hash,适合处理对象并发。
// rsvp: 对象并发
RSVP.hash({ user: p1, posts: p2 }).then(results => console.log(results));
when 提供 when.all、when.join,支持类似功能。
// when: 基础并发
when.all([p1, p2]).then(results => console.log(results));
这是选型时最关键的因素。大多数库已不再推荐用于新项目。
| 包名 | 状态 | 建议 |
|---|---|---|
bluebird | 维护中 | 旧项目可选,新项目用原生 |
lie | 维护中 | 仅用于极旧浏览器 polyfill |
q | 已弃用 | 避免使用,迁移至原生 |
rsvp | legacy | 避免使用,除非维护旧 Ember |
when | 已弃用 | 避免使用,迁移至原生 |
q 和 when 已在 npm 上标记为 deprecated。官方明确建议停止使用。继续使用会带来安全风险和兼容性问题。
// npm 警告示例 (q)
// npm WARN deprecated [email protected]: You or someone you depend on is using Q...
bluebird 虽然未弃用,但在 Node.js 10+ 和现代浏览器中,原生 Promise 性能已接近,且无需额外依赖。
这些库见证了 JavaScript 异步编程的发展史。bluebird 功能最强,但原生 Promise 已足够应对 90% 的场景。lie 仅适合特定兼容需求。q、rsvp、when 应逐步淘汰。
Promise 或 async/await。无需引入任何第三方库。bluebird。lie 或 babel-polyfill。q 或 when,优先重构为原生 Promise,减少技术债务。最终建议:除非有不可替代的特殊需求,否则不要在新架构中引入这些库。原生标准更稳定,社区支持更好,且能减少 bundle 体积。保持技术栈简洁 — 这是现代前端开发的核心原则。
选择 bluebird 仅当你需要在旧版 Node.js 环境中使用原生 Promise 不支持的高级功能,如 Promise 取消、超时控制或并发映射。它的性能优化和调试功能非常强大,但在新项目中应优先考虑原生 Promise 或 async/await。
选择 lie 仅当你必须支持 IE8 或 IE9 等极旧浏览器且无法使用 babel-polyfill 时。它是一个轻量的 Promise 规范 polyfill,没有额外功能,现代项目完全不需要引入。
不要在新项目中选择 q。该库已正式标记为 deprecated,官方建议迁移至原生 Promise。仅在对旧代码库进行维护且重构成本过高时暂时保留。
不要在新项目中选择 rsvp。它曾是 Ember.js 的核心依赖,但随着 Ember 转向原生 Promise,其独立性已大幅降低。除非维护旧的 Ember 应用,否则无需考虑。
不要在新项目中选择 when。该库已停止维护,其功能大多已被 ES6 原生 Promise 覆盖。遇到使用 when 的旧项目时,应计划逐步迁移到原生标准。
Got a question? Join us on stackoverflow, the mailing list or chat on IRC
Bluebird is a fully featured promise library with focus on innovative features and performance
See the bluebird website for further documentation, references and instructions. See the API reference here.
For bluebird 2.x documentation and files, see the 2.x tree.
Promises in Node.js 10 are significantly faster than before. Bluebird still includes a lot of features like cancellation, iteration methods and warnings that native promises don't. If you are using Bluebird for performance rather than for those - please consider giving native promises a shot and running the benchmarks yourself.
The github issue tracker is only for bug reports and feature requests. Anything else, such as questions for help in using the library, should be posted in StackOverflow under tags promise and bluebird.
Thanks to BrowserStack for providing us with a free account which lets us support old browsers like IE8.
The MIT License (MIT)
Copyright (c) 2013-2019 Petka Antonov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.