====== 差别 ======
这里会显示出您选择的修订版和当前版本之间的差别。
后一修订版 | 前一修订版 | ||
javascript_手写promise [2021/09/23 11:31] 若愚 创建 |
javascript_手写promise [2022/04/06 22:33] (当前版本) 若愚 |
||
---|---|---|---|
行 1: | 行 1: | ||
# 手写Promise | # 手写Promise | ||
- | 代码比较短的实现 | + | |
```javascript | ```javascript | ||
- | const states | + | const PENDING |
- | 0: ' | + | const FULFILLED = " |
- | 1: ' | + | const REJECTED = " |
- | | + | |
+ | function resolve(value) { | ||
+ | | ||
} | } | ||
- | function | + | |
- | | + | function |
- | | + | throw err; |
- | } | + | } |
- | let state = states[0] | + | |
- | let value = null | + | function |
- | let handlers = [] | + | if (promise2 === x) { |
- | | + | |
- | | + | new TypeError(" |
- | value = result | + | |
- | | + | |
- | | + | |
} | } | ||
- | | + | |
- | state = states[2] | + | if ((typeof x === " |
- | value = error | + | |
- | handlers.forEach(handle) | + | |
- | handlers | + | |
- | } | + | |
- | | + | |
try { | try { | ||
- | let then = getThen(value) | + | let then = x.then; |
- | if (then) { | + | if (typeof |
- | | + | then.call( |
- | return | + | x, |
+ | (y) => { | ||
+ | if (called) return; | ||
+ | called = true; | ||
+ | resolvePromise(promise2, | ||
+ | }, | ||
+ | (r) => { | ||
+ | if (called) | ||
+ | called = true; | ||
+ | reject(r); | ||
+ | } | ||
+ | ); | ||
+ | } else { | ||
+ | resolve(x); | ||
} | } | ||
- | fulfill(value) | + | |
- | | + | if (called) return; |
- | reject(err) | + | called = true; |
+ | reject(e); | ||
} | } | ||
+ | } else { | ||
+ | resolve(x); | ||
} | } | ||
- | function handle | + | } |
- | | + | |
- | | + | class Promise { |
- | if (state === states[1] && | + | constructor(executor) { |
- | | + | |
- | | + | this.value |
+ | this.reason = undefined; | ||
+ | this.resolveCallbacks | ||
+ | this.rejectCallbacks = []; | ||
+ | |||
+ | | ||
+ | if (this.status | ||
+ | this.status | ||
+ | this.value | ||
+ | | ||
} | } | ||
- | if (state === states[2] && | + | }; |
- | | + | |
- | | + | let reject = (reason) |
+ | if (this.status | ||
+ | | ||
+ | this.reason = reason; | ||
+ | this.rejectCallbacks.forEach((fn) => fn()); | ||
} | } | ||
+ | }; | ||
+ | |||
+ | try { | ||
+ | executor(resolve, | ||
+ | } catch (error) { | ||
+ | reject(error); | ||
} | } | ||
} | } | ||
- | this.done = function | + | |
- | | + | then(onFulfilled, onRejected) { |
- | } | + | |
- | this.then | + | |
- | let self = this | + | let promise2 |
- | return | + | |
- | | + | |
- | | + | |
try { | try { | ||
- | | + | |
- | } catch (err) { | + | resolvePromise(promise2, |
- | | + | } catch (e) { |
+ | reject(e); | ||
} | } | ||
- | } else { | + | }, 0); |
- | return resolve(result) | + | } |
- | } | + | |
- | }, error => { | + | |
- | if (typeof onReject | + | setTimeout(() => { |
try { | try { | ||
- | | + | |
- | } catch (err) { | + | resolvePromise(promise2, |
- | | + | } catch (e) { |
+ | reject(e); | ||
} | } | ||
- | } else { | + | }, 0); |
- | | + | } |
- | } | + | |
- | }) | + | if (this.status === PENDING) { |
- | }) | + | this.resolveCallbacks.push(() => { |
- | } | + | |
- | } | + | try { |
- | function getThen (value) { | + | let x = onFulfilled(this.value); |
- | | + | |
- | || typeof(value) === ' | + | |
- | let then = value.then | + | |
- | | + | |
- | } | + | }, 0); |
- | | + | }); |
- | } | + | |
+ | this.rejectCallbacks.push(() => { | ||
+ | | ||
+ | try { | ||
+ | | ||
+ | | ||
+ | } catch (e) { | ||
+ | reject(e); | ||
+ | } | ||
+ | }, 0); | ||
+ | }); | ||
+ | } | ||
+ | }); | ||
- | function resolveAnotherPromise (cb, Onfulfill, Onreject) { | + | |
- | let finished = false | + | |
- | try { | + | |
- | cb(value => { | + | |
- | if (finished) | + | |
- | finished = true | + | |
- | Onfulfill(value) | + | |
- | }, reason => { | + | |
- | if (finished) return | + | |
- | finished = true | + | |
- | Onreject(reason) | + | |
- | }) | + | |
- | } catch (err) { | + | |
- | if (finished) return | + | |
- | finished = true | + | |
- | Onreject(err) | + | |
} | } | ||
- | } | ||
} | } | ||
``` | ``` | ||
+ | |||
+ | 更标准的实现 | ||
+ | |||
+ | https:// | ||
+ | |||
+ | |||