====== 差别 ======
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
javascript_promisea_规范 [2022/04/06 18:17] 若愚 |
javascript_promisea_规范 [2022/04/06 18:53] (当前版本) 若愚 |
||
---|---|---|---|
行 5: | 行 5: | ||
1. https:// | 1. https:// | ||
- | ## Terminology | ||
- | 1. " | + | ## 规范解析 |
- | 1. " | + | |
- | 1. " | + | |
- | 1. " | + | |
- | 1. " | + | |
- | ## Requirements | + | #### 2.1 |
+ | ```javascript | ||
- | ### Promise | + | var p1 = new Promise(function(){}) |
- | + | var p2 = Promise.resolve(1) // | |
- | A promise must be in one of three states: | + | var p3 = Promise.reject('error') // |
- | + | ||
- | 1. When pending, a promise: | + | |
- | | + | |
- | 1. When fulfilled, a promise: | + | |
- | 1. must not transition to any other state. | + | |
- | 1. must have a value, which must not change. | + | |
- | 1. When rejected, a promise: | + | |
- | 1. must not transition to any other state. | + | |
- | 1. must have a reason, which must not change. | + | |
- | + | ||
- | Here, "must not change" | + | |
- | + | ||
- | ### 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: | + | |
- | + | ||
- | ```js | + | |
- | promise.then(onFulfilled, | + | |
``` | ``` | ||
- | 补充范例1: | + | #### 2.2 |
```javascript | ```javascript | ||
- | let p = new Promise(function(resolve, reject) { | + | new Promise((resolve, |
- | | + | |
+ | if(v > 0.5) { | ||
+ | | ||
+ | } else { | ||
+ | reject(' | ||
+ | } | ||
+ | }).then(v => { | ||
+ | console.log(v) | ||
+ | }, reason => { | ||
+ | console.error(reason) | ||
}) | }) | ||
- | p.then(function(x) {}, function(reason) {}) | ||
``` | ``` | ||
- | 1. Both `onFulfilled` and `onRejected` are optional arguments: | + | 2.2.6 |
- | 1. If `onFulfilled` is not a function, it must be ignored. | + | |
- | 1. If `onRejected` is not a function, it must be ignored. | + | |
- | 1. If `onFulfilled` is a function: | + | |
- | 1. it must be called after `promise` is fulfilled, with `promise`' | + | |
- | 1. it must not be called before `promise` is fulfilled. | + | |
- | 1. it must not be called more than once. | + | |
- | 1. If `onRejected` is a function, | + | |
- | 1. it must be called after `promise` is rejected, with `promise`' | + | |
- | 1. it must not be called before `promise` is rejected. | + | |
- | 1. it must not be called more than once. | + | |
- | 1. `onFulfilled` or `onRejected` must not be called until the [execution context](https:// | + | |
- | 1. `onFulfilled` and `onRejected` must be called as functions (i.e. with no `this` value). [[3.2](# | + | |
- | 1. `then` may be called multiple times on the same promise. | + | |
- | 1. If/when `promise` is fulfilled, all respective `onFulfilled` callbacks must execute in the order of their originating calls to `then`. | + | |
- | 1. If/when `promise` is rejected, all respective `onRejected` callbacks must execute in the order of their originating calls to `then`. | + | |
- | 1. `then` must return a promise [[3.3](# | + | |
- | | + | ```javascript |
- | | + | let p = new Promise(resolve => resolve(1)) |
- | ``` | + | p.then(v => console.log(v)) |
+ | p.then(v => console.log(v)) | ||
+ | ``` | ||
- | 1. If either `onFulfilled` or `onRejected` returns a value `x`, run the Promise Resolution Procedure `[[Resolve]](promise2, | ||
- | 1. If either `onFulfilled` or `onRejected` throws an exception `e`, `promise2` must be rejected with `e` as the reason. | ||
- | 1. If `onFulfilled` is not a function and `promise1` is fulfilled, `promise2` must be fulfilled with the same value as `promise1`. | ||
- | 1. If `onRejected` is not a function and `promise1` is rejected, `promise2` must be rejected with the same reason as `promise1`. | ||
- | ### The Promise Resolution Procedure | + | #### 2.3 |
- | The **promise resolution procedure** is an abstract operation taking as input a promise and a value, which we denote as `[[Resolve]](promise, | ||
- | This treatment of thenables allows | + | then 必须返回一个promise,对于`promise2 = promise1.then(onFulfilled, |
- | To run `[[Resolve]](promise, | + | 当x是一个普通值时 2.3.4 |
- | 1. If `promise` and `x` refer to the same object, reject `promise` with a `TypeError` as the reason. | ||
- | 1. If `x` is a promise, adopt its state [[3.4](# | ||
- | 1. If `x` is pending, `promise` must remain pending until `x` is fulfilled or rejected. | ||
- | 1. If/when `x` is fulfilled, fulfill `promise` with the same value. | ||
- | 1. If/when `x` is rejected, reject `promise` with the same reason. | ||
- | 1. Otherwise, if `x` is an object or function, | ||
- | 1. Let `then` be `x.then`. [[3.5](# | ||
- | 1. If retrieving the property `x.then` results in a thrown exception `e`, reject `promise` with `e` as the reason. | ||
- | 1. If `then` is a function, call it with `x` as `this`, first argument `resolvePromise`, | ||
- | 1. If/when `resolvePromise` is called with a value `y`, run `[[Resolve]](promise, | ||
- | 1. If/when `rejectPromise` is called with a reason `r`, reject `promise` with `r`. | ||
- | 1. If both `resolvePromise` and `rejectPromise` are called, or multiple calls to the same argument are made, the first call takes precedence, and any further calls are ignored. | ||
- | 1. If calling `then` throws an exception `e`, | ||
- | 1. If `resolvePromise` or `rejectPromise` have been called, ignore it. | ||
- | 1. Otherwise, reject `promise` with `e` as the reason. | ||
- | 1. If `then` is not a function, fulfill `promise` with `x`. | ||
- | 1. If `x` is not an object or function, fulfill `promise` with `x`. | ||
- | |||
- | If a promise is resolved with a thenable that participates in a circular thenable chain, such that the recursive nature of `[[Resolve]](promise, | ||
- | |||
- | ## Notes | ||
- | |||
- | 1. Here " | ||
- | |||
- | 1. That is, in strict mode `this` will be `undefined` inside of them; in sloppy mode, it will be the global object. | ||
- | |||
- | 1. Implementations may allow `promise2 === promise1`, provided the implementation meets all requirements. Each implementation should document whether it can produce `promise2 === promise1` and under what conditions. | ||
- | |||
- | 1. 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. | ||
- | |||
- | 1. This procedure of first storing a reference to `x.then`, then testing that reference, and then calling that reference, avoids multiple accesses to the `x.then` property. Such precautions are important for ensuring consistency in the face of an accessor property, whose value could change between retrievals. | ||
- | |||
- | 1. 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`; | ||
- | |||
- | |||
- | ## 规范解析 | ||
```javascript | ```javascript | ||
const delayer = (t) => new Promise((resolve, | const delayer = (t) => new Promise((resolve, | ||
行 127: | 行 55: | ||
}) | }) | ||
- | ``` | ||
- | |||
- | ```javascript | ||
delayer(1000).then(t => { | delayer(1000).then(t => { | ||
console.log(t) | console.log(t) | ||
- | return t + 1000 | + | return t + 1000 // |
}).then(t => { | }).then(t => { | ||
console.log(t) | console.log(t) | ||
行 139: | 行 64: | ||
}) | }) | ||
``` | ``` | ||
+ | |||
+ | |||
+ | 当x是一个Promise对象时 2.3.2 | ||
```javascript | ```javascript | ||
行 160: | 行 88: | ||
}) | }) | ||
``` | ``` | ||
+ | |||
+ | |||
+ | 当x是包含then方法的对象时 2.3.3 | ||
```javascript | ```javascript | ||
行 179: | 行 110: | ||
}) | }) | ||
``` | ``` | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ |