(promise-polyfill)手动实现一个符合Promises/A+规范的promise, 并添加Promise的常用方法

/* 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值