promise A+标准:
Promise States 承诺状态
A promise must be in one of three states: pending, fulfilled, or rejected.
承诺必须处于以下三种状态之一:待处理、已履行或已拒绝。
Here, “must not change” means immutable identity (i.e. ===), but does not imply deep immutability.
在这里,“不得改变”意味着不可变的身份(即), === 但并不意味着深刻的不变性。
The then Method 方法 then
A promise must provide a then method to access its current or eventual value or reason.
承诺必须提供一种 then 方法来访问其当前或最终的价值或原因。
A promise’s then method accepts two arguments:
promise then 的方法接受两个参数:
promise.then(onFulfilled, onRejected)
-
Both
onFulfilledandonRejectedare optional arguments:
这两个onFulfilled参数都是onRejected可选参数: -
onFulfilledoronRejectedmust not be called until the execution context stack contains only platform code. [3.1].
onFulfilled或者onRejected,在执行上下文堆栈仅包含平台代码之前,不得调用。[ 3.1]. -
onFulfilledandonRejectedmust be called as functions (i.e. with nothisvalue). [3.2]
onFulfilled并且onRejected必须作为函数调用(即没有this值)。[ 3.2] -
thenmay be called multiple times on the same promise.
then可以在同一个 promise 上多次调用。- If/when
promiseis fulfilled, all respectiveonFulfilledcallbacks must execute in the order of their originating calls tothen.
如果/何时promise满足,则所有相应的onFulfilled回调必须按照其对then的发起调用的顺序执行。 - If/when
promiseis rejected, all respectiveonRejectedcallbacks must execute in the order of their originating calls tothen.
如果/当promise被拒绝时,所有相应的onRejected回调必须按照其对then的发起调用的顺序执行。
- If/when
-
thenmust return a promise [3.3].
then必须返回一个 promise [ 3.3]。promise2 = promise1.then(onFulfilled, onRejected);- If either
onFulfilledoronRejectedreturns a valuex, run the Promise Resolution Procedure[[Resolve]](promise2, x).
如果任一onFulfilled或onRejected返回一个值x,请运行 Promise 解析过程[[Resolve]](promise2, x)。 - If either
onFulfilledoronRejectedthrows an exceptione,promise2must be rejected witheas the reason.
如果任一onFulfilled或onRejected抛出异常e,promise2则必须以e拒绝为理由。 - If
onFulfilledis not a function andpromise1is fulfilled,promise2must be fulfilled with the same value aspromise1.
如果不是onFulfilled函数,并且promise1已实现,promise2则必须使用与promise1相同的值实现。 - If
onRejectedis not a function andpromise1is rejected,promise2must be rejected with the same reason aspromise1.
如果onRejected不是函数并且promise1被拒绝,promise2则必须以与promise1相同的理由拒绝。
- 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.
promise 解析过程是一个抽象操作,将 promise 和一个值作为输入,我们将其表示为 [[Resolve]](promise, x) 。如果 x 是 thenable,则它试图在行为至少在某种程度上类似于 promise 的假设 x 下 promise 采用 的 x 状态。否则,它 promise 满足值 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.
这种对 thenables 的处理允许 promise 实现进行互操作,只要它们公开符合 Promises/A+ then 的方法即可。它还允许 Promises/A+ 实现使用合理的 then 方法“同化”不符合要求的实现。
To run [[Resolve]](promise, x), perform the following steps:
要运行 [[Resolve]](promise, x) ,请执行以下步骤:
-
If
promiseandxrefer to the same object, rejectpromisewith aTypeErroras the reason.
如果promise和x引用同一对象,则以 aTypeError作为理由拒绝promise。 -
If
xis a promise, adopt its state [3.4]:
如果x是一个承诺,则采用其状态 [ 3.4]:- If
xis pending,promisemust remain pending untilxis fulfilled or rejected.
如果x处于待处理状态,promise则必须保持待处理状态,直到x完成或被拒绝。 - If/when
xis fulfilled, fulfillpromisewith the same value.
如果/何时x实现,则以相同的价值实现promise。 - If/when
xis rejected, rejectpromisewith the same reason.
如果/当x被拒绝时,请以同样的理由拒绝promise。
- If
-
Otherwise, if
xis an object or function,
否则,如果x是一个对象或函数,-
Let
thenbex.then. [3.5]
让它then成为x.then.[ 3.5] -
If retrieving the property
x.thenresults in a thrown exceptione, rejectpromisewitheas the reason.
如果检索属性x.then导致引发异常e,则以e拒绝promise为理由。 -
If
thenis a function, call it withxasthis, first argumentresolvePromise, and second argumentrejectPromise, where:
如果then是一个函数,则使用xasthis、 第一个参数resolvePromise和第二个参数rejectPromise调用它,其中:-
If/when
resolvePromiseis called with a valuey, run[[Resolve]](promise, y).
如果/当resolvePromise用值y调用时,请运行[[Resolve]](promise, y)。 -
If/when
rejectPromiseis called with a reasonr, rejectpromisewithr.
如果/当rejectPromise被调用时有原因,拒绝promise有r原因r。 -
If both
resolvePromiseandrejectPromiseare called, or multiple calls to the same argument are made, the first call takes precedence, and any further calls are ignored.
如果同时resolvePromise调用 和rejectPromise或对同一参数进行多次调用,则第一个调用优先,任何进一步的调用都将被忽略。
-
-
If
thenis not a function, fulfillpromisewithx.
如果不是then函数,promise则使用x.
-
-
If
xis not an object or function, fulfillpromisewithx.
如果不是x对象或函数,promise则使用x.
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]
如果一个承诺被解析为一个参与循环可thenable链的thenable,使得 [[Resolve]](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
onFulfilledandonRejectedexecute asynchronously, after the event loop turn in whichthenis called, and with a fresh stack. This can be implemented with either a “macro-task” mechanism such assetTimeoutorsetImmediate, or with a “micro-task” mechanism such asMutationObserverorprocess.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.
这里的“平台代码”是指引擎、环境和 promise 实现代码。在实践中,此要求确保onFulfilled在调用的事件循环之后then异步onRejected执行,并使用新的堆栈。这可以通过“宏任务”机制(如setTimeout或setImmediate)或“微任务”机制(如MutationObserver或process.nextTick)来实现。由于 promise 实现被视为平台代码,因此它本身可能包含一个任务调度队列或“蹦床”,其中调用了处理程序。 -
That is, in strict mode
thiswill beundefinedinside of them; in sloppy mode, it will be the global object.
也就是说,在严格模式this下将在undefined其中;在草率模式下,它将是全局对象。 -
Implementations may allow
promise2 === promise1, provided the implementation meets all requirements. Each implementation should document whether it can producepromise2 === promise1and under what conditions.
实现可能允许promise2 === promise1,前提是实现满足所有要求。每个实现都应记录它是否可以产生以及在什么条件下产生promise2 === promise1。 -
Generally, it will only be known that
xis 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.
通常,只有当它来自当前的实现时,才会知道x这是一个真正的承诺。该子句允许使用特定于实现的方法来采用已知符合承诺的状态。 -
This procedure of first storing a reference to
x.then, then testing that reference, and then calling that reference, avoids multiple accesses to thex.thenproperty. Such precautions are important for ensuring consistency in the face of an accessor property, whose value could change between retrievals.
首先存储对x.then的引用,然后测试该引用,然后调用该引用的过程可避免对x.then属性的多次访问。这种预防措施对于确保访问器属性的一致性非常重要,因为访问器属性的价值可能会在检索之间发生变化。 -
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.
实现不应对可然后链的深度设置任意限制,并假设超出该任意限制,递归将是无限的。只有真正的循环才能导致TypeError;如果遇到无限条不同的 thenables 链,则永远递归是正确的行为。