/* Promises/A+ specification */
// 1. Terminology
// 1.1. “promise” is an object or function with a then method whose behavior conforms to this specification.
// 1.2. “thenable” is an object or function that defines a then method.
// 1.3. “value” is any legal JavaScript value (including undefined, a thenable, or a promise).
// 1.4. “exception” is a value that is thrown using the throw statement.
// 1.5. “reason” is a value that indicates why a promise was rejected.
// 2. Requirements
// 2.1. Promise States
// A promise must be in one of three states: pending, fulfilled, or rejected.
// 2.1.1. When pending, a promise:
// 2.1.1.1. may transition to either the fulfilled or rejected state.
// 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.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.
// 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.1. Both onFulfilled and onRejected are optional arguments:
// 2.2.1.1. If onFulfilled is not a function, it must be ignored.
// 2.2.1.2. If onRejected is not a function, it must be ignored.
// 2.2.2. If onFulfilled is a function:
// 2.2.2.1. it must be called after promise is fulfilled, with promise’s value as its first argument.
// 2.2.2.2. it must not be called before promise is fulfilled.
// 2.2.2.3. it must not be called more than once.
// 2.2.3. If onRejected is a function,
// 2.2.3.1. it must be called after promise is rejected, with promise’s reason as its first argument.
// 2.2.3.2. it must not be called before promise is rejected.
// 2.2.3.3. it must not be called more than once.
// 2.2.4. onFulfilled or onRejected must not be called until the execution context stack contains only platform code. [3.1].
// 2.2.5. onFulfilled and onRejected must be called as functions (i.e. with no this value). [3.2]
// 2.2.6. then may be called multiple times on the same promise.
// 2.2.6.1. If/when promise is fulfilled, all respective onFulfilled callbacks must execute in the order of their originating calls to then.
// 2.2.6.2. If/when promise is rejected, all respective onRejected callbacks must execute in the order of their originating calls to then.
// 2.2.7. then must return a promise [3.3].
// promise2 = promise1.then(onFulfilled, onRejected);
// 2.2.7.1. If either onFulfilled or onRejected returns a value x, run the Promise Resolution Procedure [[Resolve]](promise2, x).
// 2.2.7.2. If
(promise-polyfill)手动实现一个符合Promises/A+规范的promise, 并添加Promise的常用方法
最新推荐文章于 2024-03-23 09:57:31 发布