原文
An open standard for sound, interoperable JavaScript promises—by implementers, for 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.
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.
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.
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.
Terminology
- “promise” is an object or function with a
then
method whose behavior conforms to this specification. - “thenable” is an object or function that defines a
then
method. - “value” is any legal JavaScript value (including
undefined
, a thenable, or a promise). - “exception” is a value that is thrown using the
throw
statement. - “reason” is a value that indicates why a promise was rejected.
Requirements
Promise States
A promise must be in one of three states: pending, fulfilled, or rejected.
- When pending, a promise:
- may transition to either the fulfilled or rejected state.
- When fulfilled, a promise:
- must not transition to any other state.
- must have a value, which must not change.
- When rejected, a promise:
- must not transition to any other state.
- must have a reason, which must not change.
Here, “must not change” means immutable identity (i.e. ===
), but does not imply deep immutability.
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)
-
Both
onFulfilled
andonRejected
are optional arguments:- If
onFulfilled
is not a function, it must be ignored. - If
onRejected
is not a function, it must be ignored.
- If
-
If
onFulfilled
is a function:- it must be called after
promise
is fulfilled, withpromise
’s value as its first argument. - it must not be called before
promise
is fulfilled. - it must not be called more than once.
- it must be called after
-
If
onRejected
is a function,- it must be called after
promise
is rejected, withpromise
’s reason as its first argument. - it must not be called before
promise
is rejected. - it must not be called more than once.
- it must be called after
onFulfilled
oronRejected
must not be called until the execution context stack contains only platform code. [3.1].onFulfilled
andonRejected
must be called as functions (i.e. with nothis
value). [3.2]-
then
may be called multiple times on the same promise.- If/when
promise
is fulfilled, all respectiveonFulfilled
callbacks must execute in the order of their originating calls tothen
. - If/when
promise
is rejected, all respectiveonRejected
callbacks must execute in the order of their originating calls tothen
.
- If/when
-
then
must return a promise [3.3].promise2 = promise1.then(onFulfilled, onRejected);
- If either
onFulfilled
oronRejected
returns a valuex
, run the Promise Resolution Procedure[[Resolve]](promise2, x)
. - If either
onFulfilled
oronRejected
throws an exceptione
,promise2
must be rejected withe
as the reason. - If
onFulfilled
is not a function andpromise1
is fulfilled,promise2
must be fulfilled with the same value aspromise1
. - If
onRejected
is not a function andpromise1
is rejected,promise2
must be rejected with the same reason aspromise1
.
- If either
The Promise Resolution Procedure
The promise resolution procedure is an abstract operation taking as input a promise and a value, which we denote as [[Resolve]](promise, x)
. If x
is a thenable, it attempts to make promise
adopt the state of x
, under the assumption that x
behaves at least somewhat like a promise. Otherwise, it fulfills promise
with the value x
.
This treatment of thenables allows promise implementations to interoperate, as long as they expose a Promises/A+-compliant then
method. It also allows Promises/A+ implementations to “assimilate” nonconformant implementations with reasonable then
methods.
To run [[Resolve]](promise, x)
, perform the following steps:
- If
promise
andx
refer to the same object, rejectpromise
with aTypeError
as the reason. - If
x
is a promise, adopt its state [3.4]:- If
x
is pending,promise
must remain pending untilx
is fulfilled or rejected. - If/when
x
is fulfilled, fulfillpromise
with the same value. - If/when
x
is rejected, rejectpromise
with the same reason.
- If
- Otherwise, if
x
is an object or function,- Let
then
bex.then
. [3.5] - If retrieving the property
x.then
results in a thrown exceptione
, rejectpromise
withe
as the reason. - If
then
is a function, call it withx
asthis
, first argumentresolvePromise
, and second argumentrejectPromise
, where:- If/when
resolvePromise
is called with a valuey
, run[[Resolve]](promise, y)
. - If/when
rejectPromise
is called with a reasonr
, rejectpromise
withr
. - If both
resolvePromise
andrejectPromise
are called, or multiple calls to the same argument are made, the first call takes precedence, and any further calls are ignored. - If calling
then
throws an exceptione
,- If
resolvePromise
orrejectPromise
have been called, ignore it. - Otherwise, reject
promise
withe
as the reason.
- If
- If/when
- If
then
is not a function, fulfillpromise
withx
.
- Let
- If
x
is not an object or function, fulfillpromise
withx
.
If a promise is resolved with a thenable that participates in a circular thenable chain, such that the recursive nature of [[Resolve]](promise, thenable)
eventually causes [[Resolve]](promise, thenable)
to be called again, following the above algorithm will lead to infinite recursion. Implementations are encouraged, but not required, to detect such recursion and reject promise
with an informative TypeError
as the reason. [3.6]
Notes
-
Here “platform code” means engine, environment, and promise implementation code. In practice, this requirement ensures that
onFulfilled
andonRejected
execute asynchronously, after the event loop turn in whichthen
is called, and with a fresh stack. This can be implemented with either a “macro-task” mechanism such assetTimeout
orsetImmediate
, or with a “micro-task” mechanism such asMutationObserver
orprocess.nextTick
. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or “trampoline” in which the handlers are called. -
That is, in strict mode
this
will beundefined
inside of them; in sloppy mode, it will be the global object. -
Implementations may allow
promise2 === promise1
, provided the implementation meets all requirements. Each implementation should document whether it can producepromise2 === promise1
and under what conditions. -
Generally, it will only be known that
x
is a true promise if it comes from the current implementation. This clause allows the use of implementation-specific means to adopt the state of known-conformant promises. -
This procedure of first storing a reference to
x.then
, then testing that reference, and then calling that reference, avoids multiple accesses to thex.then
property. Such precautions are important for ensuring consistency in the face of an accessor property, whose value could change between retrievals. -
Implementations should not set arbitrary limits on the depth of thenable chains, and assume that beyond that arbitrary limit the recursion will be infinite. Only true cycles should lead to a
TypeError
; if an infinite chain of distinct thenables is encountered, recursing forever is the correct behavior.
中文译文
由开发者制定的开放、通用可信任的的promise标准,供开发者参考
promise代表了一个异步操作的最终结果。promise最主要的交互方式是通过它的 then
方法,这个方法注册了回调函数用来接受一个promise的最终value或者被reject的reason。
该规范详细说明了then
方法的行为,为所有符合promise/A+规范实现的promise均可以本标准作为参照基础来实施 then 方法。因为这个规则十分稳定。尽管Promises / A +组织有时会通过向后兼容的微小更改来修订此规范,以解决新发现的极端情况,但只有在仔细考虑,讨论和测试之后,我们才会集成大型或向后不兼容的更改。
从历史上看,Promises / A +阐明了较早Promises / A提案的行为条款,将其扩展为涵盖事实上的行为,并省略了未指定或有问题的部分。
最后,核心的Promises / A +规范不涉及如何create,fulfill或reject一个promise,而是选择专注于提供可互操作的then
方法。 规范中的未来工作可能涉及这些主题。
术语
"promise"是一个对象或函数,它拥有符合此规范的
then
方法"thenable"是一个对象或者方法,它定义了一个then方法。
"value"是一个任意合法javascript值(可以是'undefined',thenable或promise)
"exception"是通过
throw
声明抛出的一个错误"reason"表明了一个promise失败的原因。
要求
Promise 状态
一个promise 必须处于以下三个状态种的一种: pending,fulfilled或rejecged
- 在pending状态时,promise:
- 可以转变为fulfilled或rejected状态。
- 当fulfilled状态时, a promise:
- 无法转换到其他状态。
- 一定会包含一个value,并且无法改变
- 当rejected状态时,promise:
- 不能转换到其他状态
- 必定有reason,并且无法改变
在这里,“不能转换”意味着恒等(可以用===判断),但不适用于深层的改变(如果value或reason不为基本类型,可能会修改属性值)。
then
方法
promise必须提供一个then
方法来访问它的当前值或最终value/reason。
promise的then
方法接收2个参数值:
promise.then(onFulfilled, onRejected)
-
onFulfilled
和onRejected
都是可选参数:-
onFulfilled
如果不为函数,则会被忽略执行 -
onRejected
如果不为函数,则会被忽略执行
-
-
onFulfilled
如果是函数:- 在promise变为fulfilled之后才会被调用,
promise
的value会时它的第一个参数值。 - 在promise变成fulfilled之前都不会被调用。
- 不能被调用超过一次。
- 在promise变为fulfilled之后才会被调用,
-
onRejected
如果是一个函数- 一定会在promise状态变成rejected之后调用,promise的reason是它第一个参数。
- 在promise状态没有变为rejected之前不会被调用。
- [](https://promisesaplus.com/#point-33)无法被调用超过一次。
onFulfilled
和onRejected
只有在执行环境堆栈仅包含平台代码时才可被调用
[3.1].onFulfilled
和onRejected
必须被用作函数调用 (i.e. with nothis
value). [3.2]-
then
允许在一个promise中调用多次- 当promise状态变为fulfilled,所有
onFullfilled
回调必须按照初始调用then
方法的先后顺序执行。 - 当promise状态变为rejected,所有
onRejected
回调必须按照初始调用then
方法的先后顺序执行。
- 当promise状态变为fulfilled,所有
-
then
方法必定返回一个promise [3.3].promise2 = promise1.then(onFulfilled, onRejected);
- 如果 onFulfilled 或者 onRejected 返回一个值 x ,则运行下面的 Promise 解决过程:[[Resolve]](promise2, x)
如果
onFulfilled
或onRejected
抛出一个一场e
,promise2
必定返回e作为一reason。如果
onFulfilled
不是一个函数并且promise1
是fulfilled状态,promise2
必定和promise1
有同样的value。如果
onRejected
不是一个函数并且promise1
处于rejected状态,promise2
与promise1
有相同的rason值
Promise解决过程
promise 解决过程是一个抽象操作,它输入一个promise和一个value,我们可以表示为 [[Resolve]](promise, x)
. 如果 x 有 then 方法且看上去像一个 Promise ,解决程序即尝试使 promise 接受 x 的状态;否则其用 x 的值来执行 promise 。
这种 thenable 的特性使得 Promise 的实现更具有通用性:只要其暴露出一个遵循 Promise/A+ 协议的 then 方法即可;这同时也使遵循 Promise/A+ 规范的实现可以与那些不太规范但可用的实现能良好共存。
运行 [[Resolve]](promise, x) 需遵循以下步骤
- 如果
promise
和x
指向同一个对象,使用TypeError
来将promise该为rejected。 - 如果 x 为 Promise ,则使 promise 接受 x 的状态 [3.4]:
- 如果 x 处于pending, promise 需保持为等待态直至 x 被执行或拒绝
- 如果 x 处于fulfiiled,用相同的值执行 promise
- 如果 x 处于rejected,用相同的值拒绝 promise
- 如果
x
是一个对象或函数- 把 x.then 赋值给 then [3.5]
- 如果取 x.then 的值时抛出错误 e ,则以 e 为据因拒绝 promise
- 果 then 是函数,将 x 作为函数的作用域 this 调用之。传递两个回调函数作为参数,第一个参数叫做 resolvePromise ,第二个参数叫做 rejectPromise:
- 如果 resolvePromise 以值 y 为参数被调用,则运行 [[Resolve]](promise, y)
- 如果 rejectPromise 以据因 r 为参数被调用,则以据因 r 拒绝 promise
- 如果 resolvePromise 和 rejectPromise 均被调用,或者被同一参数调用了多次,则优先采用首次调用并忽略剩下的调用
- 如果调用 then 方法抛出了异常 e:
- I如果 resolvePromise 或 rejectPromise 已经被调用,则忽略之
- 否则以 e 为据因拒绝 promise
- 如果 then 不是函数,以 x 为参数执行 promise
- 如果 x 不为对象或者函数,以 x 为参数执行 promise
如果一个 promise 被一个循环的 thenable 链中的对象解决,而 [[Resolve]](promise, thenable) 的递归性质又使得其被再次调用,根据上述的算法将会陷入无限递归之中。算法虽不强制要求,但也鼓励施者检测这样的递归是否存在,若检测到存在则以一个可识别的 TypeError 为据因来拒绝 promise[3.6]
Notes
-
Here “platform code” means engine, environment, and promise implementation code. In practice, this requirement ensures that
onFulfilled
andonRejected
execute asynchronously, after the event loop turn in whichthen
is called, and with a fresh stack. This can be implemented with either a “macro-task” mechanism such assetTimeout
orsetImmediate
, or with a “micro-task” mechanism such asMutationObserver
orprocess.nextTick
. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or “trampoline” in which the handlers are called. -
That is, in strict mode
this
will beundefined
inside of them; in sloppy mode, it will be the global object. -
Implementations may allow
promise2 === promise1
, provided the implementation meets all requirements. Each implementation should document whether it can producepromise2 === promise1
and under what conditions. -
Generally, it will only be known that
x
is a true promise if it comes from the current implementation. This clause allows the use of implementation-specific means to adopt the state of known-conformant promises. -
This procedure of first storing a reference to
x.then
, then testing that reference, and then calling that reference, avoids multiple accesses to thex.then
property. Such precautions are important for ensuring consistency in the face of an accessor property, whose value could change between retrievals. -
Implementations should not set arbitrary limits on the depth of thenable chains, and assume that beyond that arbitrary limit the recursion will be infinite. Only true cycles should lead to a
TypeError
; if an infinite chain of distinct thenables is encountered, recursing forever is the correct behavior.