bluebird、lie、q、rsvp、when は、ES6 でネイティブ Promise が標準化される以前に登場した非同期処理ライブラリです。これらは当時、ブラウザ間の差異を吸収したり、ネイティブより豊富な機能を提供したりするために使われました。しかし現在では、ほとんどの環境でネイティブ Promise が利用可能であり、これらのライブラリはレガシーシステムの維持や、特定の_polyfill_ が必要な古い環境を除き、新規プロジェクトでの採用は推奨されません。特に q や when は公式に非推奨(deprecated)となっています。
これら 5 つのライブラリは、JavaScript の非同期処理を標準化する前に登場した「レガシー Promise」です。現在ではネイティブ Promise が標準仕様であり、これらを使う理由はほぼありません。しかし、既存システムの維持や、特定の環境での polyfill 必要性を理解するために、技術的な違いを比較します。
最も重要な点は、これらのライブラリの多くが既に役割を終えていることです。
q
公式に非推奨です。npm ページにも deprecated 表示があります。
// q: 非推奨パッケージ
// 新規プロジェクトでは使用不可
const Q = require('q');
when
メンテナンスが終了しており、更新は停止しています。
// when: メンテナンス終了
// セキュリティ更新は期待できません
const when = require('when');
bluebird
機能は安定していますが、ネイティブ Promise で代替可能です。
// bluebird: 安定だが過剰機能
// 特定の拡張機能が必要な場合のみ使用
const Promise = require('bluebird');
rsvp
Ember.js 由来ですが、現在はネイティブ化されています。
// rsvp: レガシー
// 単独での使用は推奨されません
const RSVP = require('rsvp');
lie
古いブラウザ向け polyfill です。
// lie: polyfill
// 現代のブラウザでは不要
require('lie/polyfill');
各ライブラリは Promise インスタンスの作成方法に違いがあります。ネイティブとの互換性を確認します。
bluebird
ネイティブと同じ構文が使えます。
// bluebird: ネイティブ準拠
const promise = new Promise((resolve, reject) => {
resolve('done');
});
lie
ネイティブの polyfill なので構文は同じです。
// lie: polyfill
const promise = new Promise((resolve, reject) => {
resolve('done');
});
q
defer オブジェクトを使う古いパターンが主流です。
// q: defer パターン
const deferred = Q.defer();
const promise = deferred.promise;
deferred.resolve('done');
rsvp
ネームスペース付きのコンストラクタを使います。
// rsvp: ネームスペース
const promise = new RSVP.Promise((resolve, reject) => {
resolve('done');
});
when
関数呼び出しで生成するスタイルです。
// when: 関数生成
const promise = when.promise((resolve, reject) => {
resolve('done');
});
エラーを捕捉する方法もライブラリによって異なります。ネイティブの .catch() が標準です。
bluebird
ネイティブと同じ .catch() が使えます。
// bluebird: .catch()
promise.catch(err => {
console.error(err);
});
lie
polyfill なので .catch() が使えます。
// lie: .catch()
promise.catch(err => {
console.error(err);
});
q
.fail() メソッドを使うことが多く、.then() の第 2 引数も使われます。
// q: .fail() または .then(null, err)
promise.fail(err => {
console.error(err);
});
rsvp
.catch() が使えますが、古いバージョンでは .then() の第 2 引数を使うこともありました。
// rsvp: .catch()
promise.catch(err => {
console.error(err);
});
when
.otherwise() メソッドを使うのが特徴的です。
// when: .otherwise()
promise.otherwise(err => {
console.error(err);
});
ネイティブ Promise にない機能を提供しているかが重要な違いです。
bluebird
.map()、.timeout()、キャンセル機能など豊富です。
// bluebird: 拡張機能
Promise.map(items, item => process(item))
.timeout(5000);
lie
polyfill なので拡張機能はありません。
// lie: 拡張機能なし
// ネイティブ相当のみ
Promise.resolve('data');
q
Q.all() や Q.fcall() などの静的メソッドがあります。
// q: 静的メソッド
Q.all([promise1, promise2])
.then(results => {});
rsvp
RSVP.all() など、ネイティブに近い静的メソッドがあります。
// rsvp: 静的メソッド
RSVP.all([promise1, promise2])
.then(results => {});
when
when.join() や when.map() などのユーティリティがあります。
// when: ユーティリティ
when.map(items, item => process(item))
.then(results => {});
これらはすべて同じ時代の課題を解決しようとしていました。
// 共通:チェーン処理
promise.then(step1).then(step2);
// 共通:polyfill 的役割
// 古い環境でも Promise として動作
// 共通:エラー伝播
promise.then(() => { throw err; }).catch(handle);
| 機能 | bluebird | q | when | rsvp | lie |
|---|---|---|---|---|---|
| 状態 | 安定(レガシー) | 非推奨 | 終了 | レガシー | polyfill |
| 生成 | new Promise | Q.defer() | when.promise() | new RSVP.Promise | new Promise |
| エラー | .catch() | .fail() | .otherwise() | .catch() | .catch() |
| 拡張 | 豊富 | あり | あり | 最小限 | なし |
| 推奨 | 限定的 | ❌ | ❌ | ❌ | 環境依存 |
新規プロジェクト ネイティブ Promise を使用してください。これらライブラリを入れる理由はありません。バンドルサイズが増え、保守コストが増加します。
既存プロジェクト
q や when を使っている場合は、移行計画を立ててください。bluebird の拡張機能(.map() など)を使っている場合は、ネイティブの Promise.all() や async/await で代替できないか検討します。
polyfill が必要な場合
IE11 対応など、どうしても polyfill が必要な場合は lie よりも、core-js や promise-polyfill などの現代的な選択肢を検討してください。
最終的なアドバイス これらのライブラリは JavaScript の歴史において重要な役割を果たしましたが — 現在は役目を終えています。現代のアーキテクチャでは、ネイティブ機能を活用し、依存パッケージを減らすことが最優先です。
新規プロジェクトではネイティブ Promise を推奨しますが、既存コードで .map() や .timeout() などの拡張機能に依存している場合のみ選択します。機能は豊富ですが、バンドルサイズが大きくなるため、必要な機能だけを検討してください。
IE11 など、Promise をサポートしていない極めて古いブラウザ向け_polyfill_ としてのみ検討します。現代のウェブ開発ではネイティブ機能で十分であり、基本的に不要です。
公式に非推奨(deprecated)となっているため、新規プロジェクトでは絶対に使用しないでください。既存プロジェクトにある場合は、ネイティブ Promise への移行計画を立てるべきです。
Ember.js の古いバージョンで使用されていましたが、現在はネイティブ Promise に置き換わっています。 standalone での使用は推奨されず、 Ember のレガシーコード維持以外で選ぶ理由はありません。
メンテナンスが終了しており、セキュリティリスクとなる可能性があります。新規プロジェクトでの使用は避け、既存利用箇所も早急にネイティブ Promise へ移行してください。
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.