javascript - Promises/A+ 实现有何不同?

标签 javascript promise q bluebird rsvp-promise

规范涵盖了 promise 库的哪些方面?实现之间有哪些不同?

请举例说明实际差异(例如 Bluebird 和 Q 之间的差异)。

最佳答案

几乎所有。 Promises/A+ 规范旨在实现 promise 互操作性,它的构建使 promise 库(以及现在的原生 promises)可以相互交谈。这个想法是为了可以预测 promise 的行为方式并定义 promises 如何被其他库同化

引用规范:

This specification details the behavior of the then method, providing an interoperable base which all Promises/A+ conformant promise implementations can be depended on to provide. As such, the specification should be considered very stable. Although the Promises/A+ organization may occasionally revise this specification with minor backward-compatible changes to address newly-discovered corner cases, we will integrate large or backward-incompatible only after careful consideration, discussion, and testing. Finally, the core Promises/A+ specification does not deal with how to create, fulfill, or reject promises, choosing instead to focus on providing an interoperable then method. Future work in companion specifications may touch on these subjects.

以下内容不包括在内:

  • 创建 promise (即 promise constructor 规范)。
  • Promise 聚合(虽然大多数实现都支持 .all)。
  • 进展(即 progression spec ,我很快就会将其替换)。
  • 取消(即 cancellation spec )。
  • 未处理的拒绝监控(没有规范,但有检查讨论)。
  • 堆栈跟踪。

例如,Bluebird 和 Q 都完全是 Promises/A+ 投诉,​​但在很多方面有所不同:

  • 下一个 Q,v2 引入了估计,其中 Bluebird 打算最终弃用 progression 以支持 C# 的 IProgress 之类的东西。
  • 创建一个 promise 通常是在 Q 中使用延迟完成的(尽管它现在提供了一个 promise 构造函数变体),Bluebird 鼓励使用 promise 构造函数。
  • Bluebird 具有更强大、更强大的 promise 能力,可在单个命令中将整个回调 API 转换为 promise 。 Q 作者 Kris 构建了 Q-IO,它手动提供文件系统和 http 模块。
  • Bluebird 允许通过 .bind 和 promise 数组方法(.map,.reduce.filter 等)。
  • Q 具有异步队列和通过 Q 连接的 RPC 等原语,
  • Bluebird 大约快 100 倍,具有更好的堆栈跟踪和自动未处理拒绝检测。它还会根据 promise 消耗更少的 RAM 内存。

Here is another reference

关于javascript - Promises/A+ 实现有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403814/

相关文章:

javascript - 如何等待 Promise.all 中的 2 个 Promise 完成然后执行另一项任务

javascript - Q 未处理的拒绝原因与 Q.all

javascript - $q.all 在收到 404 响应时中止所有 promise

javascript - 在ajax调用中访问函数外部变量的问题

javascript - 为什么 Javascript ES6 中没有定义 `i` ?

javascript - 如何使 JqGrid 编辑表单做出响应

javascript - 如何使用 RxJS 将 get .pipe takeUntil 和订阅到 Angular 中的一个函数中?

javascript - 使用 bluebird Promise 时,then() 没有被调用

javascript - 如何通过中间阶段传递 promise 结果

javascript - 使用 deferred.resolve(value) 返回到我的 .then() 的值未定义