文章目录
- 1 定义整体结构
- 2 Promise 构造函数的实现
- 3 promise.then()与promise.catch()的实现
- 4 Promise.resolve()与Promise.reject()的实现
- 5 Promise.all与Promise.race()的实现
- 6 Promise.resolveDelay()与Promise.rejectDelay()的实现
1 定义整体结构
(function (window) {
function Promise(excutor) {
}
Promise.prototype.then = function (onResolved, onRejected) {
}
Promise.prototype.catch = function (onRejected) {
}
Promise.resolve = function (value) {
}
Promise.reject = function (reason) {
}
Promise.all = function (promises) {
}
Promise.race = function (promises) {
}
window.Promise = Promise
})(window)
2 Promise 构造函数的实现
function Promise(excutor) {
const self = this
self.status = 'pending'
resolved, 失败了变为 rejected
self.data = undefined
self.callbacks = []
function resolve(value) {
if(self.status!=='pending') {
return
}
self.status = 'resolved'
self.data = value
if (self.callbacks.length>0) {
setTimeout(() => {
self.callbacks.forEach(obj => {
obj.onResolved(value)
})
})
}
}
function reject(reason) {
if(self.status!=='pending') {
return
}
self.status = 'rejected'
self.data = reason
setTimeout(() => {
self.callbacks.forEach(obj => {
obj.onRejected(reason)
})
})
}
try {
excutor(resolve, reject)
} catch (error) {
reject(error)
}
}
3 promise.then()与promise.catch()的实现
Promise.prototype.then = function (onResolved, onRejected) {
const self = this
onResolved = typeof onResolved==='function' ? onResolved : value => value
onRejected = typeof onRejected === 'function' ? onRejected : reason => {throw reason}
return new Promise((resolve, reject) => {
function handle(callback) {
try {
const x = callback(self.data)
if (x instanceof Promise) {
x.then(resolve, reject)
} else {
resolve(x)
}
} catch (error) {
reject(error)
}
}
if (self.status === 'resolved') {
setTimeout(() => {
handle(onResolved)
})
} else if (self.status === 'rejected') {
setTimeout(() => {
handle(onRejected)
})
} else {
self.callbacks.push({
onResolved(value) {
handle(onResolved)
},
onRejected(reason) {
handle(onRejected)
}
})
}
})
}
4 Promise.resolve()与Promise.reject()的实现
Promise.resolve = function (value) {
return new Promise((resolve, reject) => {
if (value instanceof Promise) {
value.then(resolve, reject)
} else {
resolve(value)
}
})
}
Promise.reject = function (reason) {
return new Promise((resolve, reject) => {
reject(reason)
})
}
5 Promise.all与Promise.race()的实现
Promise.all = function (promises) {
return new Promise((resolve, reject) => {
let resolvedCount = 0
const promisesLength = promises.length
const values = new Array(promisesLength)
for (let i = 0; i < promisesLength; i++) {
Promise.resolve(promises[i]).then(
value => {
values[i] = value
resolvedCount++
if(resolvedCount===promisesLength) {
resolve(values)
}
},
reason => {
reject(reason)
}
)
}
})
}
Promise.race = function (promises) {
return new Promise((resolve, reject) => {
for (var i = 0; i < promises.length; i++) {
Promise.resolve(promises[i]).then(
(value) => {
resolve(value)
},
(reason) => {
reject(reason)
}
)
}
})
}
6 Promise.resolveDelay()与Promise.rejectDelay()的实现
Promise.resolveDelay = function (value, time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (value instanceof Promise) {
value.then(resolve, reject)
} else {
resolve(value)
}
}, time);
})
}
Promise.rejectDelay = function (reason, time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(reason)
}, time)
})
}