本文最后更新于 2024-03-23T16:32:40+00:00
手写代码的思路为:先从使用开始进行分析。
Promise 的基本使用:
1 2 3 4 5 6 7 8 9
| new Promise((resolve, reject) => { setTimeout(() => { resolve('ok') }, 2000) }).then(res => { console.log(res) }, err => { console.log(err) })
|
根据基本使用,再写出注释:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
function MyPromise(fn) { this.state = 'pending'
this.value = ''
this.thenSuccessFun = [] this.thenFailFunFun = []
const resolve = (res) => { this.state = 'fulfilled' this.value = res
if(this.thenSuccessFun.length) { this.thenSuccessFun.forEach(fn => fn(this.value)) } }
const reject = (err) => { this.state = 'rejected' this.value = err
if(this.thenFailFunFun.length) { this.thenFailFunFun.forEach(fn => fn(this.value)) } }
fn(resolve, reject)
this.then = (successFun, failFun) => { if(this.state === 'pending') { if(typeof successFun === 'function') { this.thenSuccessFun.push(successFun) }
if(typeof failFun === 'function') { this.thenFailFunFun.push(failFun) }
} else if(this.state === 'fulfilled') { if(typeof successFun === 'function') { successFun(this.value) }
} else { if(typeof failFun === 'function') { failFun(this.value) } } } }
new MyPromise((resolve, reject) => { setTimeout(() => { resolve('ok') }, 2000) }).then(res => { console.log(res) }, err => { console.log(err) })
|
上述展示了最基本的使用的实现,但是该实现与源码差的很远,只是让大家入个门,还要考虑各种边界与能力。
其他静态方法的实现原理:
**Promise.resolve(anyValue):Promise**
:成功的返回
1 2 3 4 5 6 7 8 9
| Promise.resolve = (value) => { if(value instanceof Promise) return value
return new Promise((resolve) => { resolve(value) }) }
|
**Promise.reject(anyValue):Promise**
:失败的返回
1 2 3 4 5 6 7 8 9
| Promise.reject = (value) => { if(value instanceof Promise) return value
return new Promise((resolve, reject) => { reject(value) }) }
|
**Promise.all(Array<Promise>):Promise**
:当所有的 Promise 成功时,则返回成功值的数组,否则返回第一个失败的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Promise.all = (PromiseArray) => { return new Promise((resolve, reject) => { const result = []
PromiseArray.forEach((promise, index) => {
Promise.resolve(promise).then(res => { result.push(res)
if(result.length - 1 === index) resolve(result) }).catch(err => { reject(err) }) }) }) }
|
**Promise.allSettled(Array<Promise>):Promise**
:当所有的 Promise 执行完时,返回所有结果的数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Promise.allSettled = (PromiseArray) => { return new Promise((resolve, reject) => { const result = []
PromiseArray.forEach((promise, index) => { Promise.resolve(promise).then(res => { result.push({state: 'success', value: res})
if(result.length - 1 === index) resolve(result) }).catch(err => { result.push({state: 'fali', value: err})
if(result.length - 1 === index) resolve(result) }) }) }) }
|
**Promise.race(Array<Promise>):Promise**
:返回最先执行成功的值
1 2 3 4 5 6 7 8 9
| Promise.allSettled = (PromiseArray) => { return new Promise((resolve, reject) => { PromiseArray.forEach((promise, index) => { Promise.resolve(promise).then(resolve).catch(reject) }) }) }
|