英文官方文档原文:promisesaplus.com/

前言

写本文的目的,是为了更好的理解promise,通过解读翻译原文,逐行解析原文通过代码一行一行实现。希望通过这篇文章,让我们能对promise有更深入的了解。

首先介绍promises是什么,英文的字面意思是“承诺”的意思,接下来promises翻译我没有用承诺翻译这个单词,因为我觉得有些英文只是一个词汇,还是直接用英文原文叫法好。

promise的是解决回调的问题的,通过then的链式调用,让我们能更清晰的理解阅读代码。下面直接看原文解读:

英: An open standard for sound, interoperable JavaScript promises—by implementers, for implementers.

中: 一个开源标准,可与JS互操作的Promises。由 implementers(语意创作这个promises的人或团体)创作,implementers(实现者)。

英: A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise’s eventual value or the reason why the promise cannot be fulfilled.

中: 一个promise代表了异步操作的最终结果。与一个promise完成交互的主要方法是then方法,该方法会记录回调以接收一个promise的成功返回值或者失败的原因。

英: 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 changes only after careful consideration, discussion, and testing.

中: 本规范详细的描述了then方法的行为,提供了一个可互操作的基础,所有的Promises / A +符合promise实现都可依赖提供。因此,本规范应该被认为是非常稳定的。虽然Promises / A +组织偶尔会修改这个规范,但向后兼容更改次数较少,主要解决新发现的案例,我们只有在仔细考虑,讨论以及测试之后才会整合大的改动或者向后兼容的更改。

英: Historically, Promises/A+ clarifies the behavioral clauses of the earlier Promises/A proposal, extending it to cover de facto behaviors and omitting parts that are underspecified or problematic.

中: 历史上,Promises / A +阐明了先前Promises / A提案的行为条款,将其扩展为涵盖事实上的行为,并略去了未指定或存在问题的部分。

英: 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.

中: 最后,核心Promises / A+规范不涉及如何创建,履行或拒绝promises,而是选择专注于提供可互操作的方法。未来的配套规范工作可能涉及这些主题。

英: 1.Terminology

中: 1、术语

英: 1.1 “promise” is an object or function with a then method whose behavior conforms to this specification.

中: 1.1 “promise”是一个对象或函数,它的行为符合这个规范。

英: 1.2 “thenable” is an object or function that defines a then method.

中: 1.2 “thenable”是定义then方法的对象或函数。

英: 1.3 “value” is any legal JavaScript value (including undefined, a thenable, or a promise).

中: 1.3 “value”是任何合法的JavaScript值(包括undefined,thenable或promise)。

英: 1.4 “exception” is a value that is thrown using the throw statement.

中: 1.4 “异常”是使用throw语句抛出的值。

英: 1.5 “reason” is a value that indicates why a promise was rejected.

中: 1.5 “原因”是一个值(结果)表明promise被拒绝的原因。

英: 2.Requirements

2.1.Promise States

中: 2、要求

2.1.Promise状态

英: A promise must be in one of three states: pending, fulfilled, or rejected.

中: 一个promise必须包含初始态, 成功(完成)态, 或者失败(拒绝)态这三个状态中的一种。

英: 2.1.1 When pending, a promise:
2.1.1.1 may transition to either the fulfilled or rejected state.

中: 2.1.1 当状态是初始态, promise:
2.1.1.1 可能转换到成功态或失败态。

英: 2.1.2. When fulfilled, a promise:
2.1.2.1. must not transition to any other state.
2.1.2.2. must have a value, which must not change.

中: 2.1.2 当状态是成功态,promise:
2.1.2.1 不能更改成别的状态。
2.1.2.2 必须有个不能更改的值(结果)

英: 2.1.3. When rejected, a promise:
2.1.3.1.must not transition to any other state.
2.1.3.2. must have a reason, which must not change.

中: 2.1.3 当状态是失败态,promise:
2.1.3.1. 不能更改成别的状态。
2.1.3.2. 必须有个不能更改的失败(错误)原因

英: Here, “must not change” means immutable identity (i.e. ===), but does not imply deep immutability.

中: 上面,“不能改变”的意思是不可改变的状态(即 ===),但并不意味着深不可变。

英: 2.2.The then Method
A promise must provide a then method to access its current or eventual value or reason.
A promise’s then method accepts two arguments:
promise.then(onFulfilled, onRejected)

中: 2.2 then方法
一个promise必须有一个then方法来获取成功的值(结果)或失败(错误)的原因。
一个promise方法接收两个参数:
promise.then(onFulfilled, onRejected)

英: 2.2.1. Both onFulfilled and onRejected are optional arguments:
2.2.1.1.If onFulfilled is not a function, it must be ignored.
2.