Promise的简单实现
Promise本意是承诺.主要目的是为了解决在需要多个异步操作的时候.产生的回调嵌套.在es6没出来前.用bluebird来实现.与javas中的Q模块类似.
Promise对象实例创建的过程中有三种状态, Pending、Fulfilled、Rejected. Fulfilled与Rejected间不能相互转换
promise的使用与原理实现
then
promise.then用于链式调用,每次执行时都会返回一个新的promise对象
all
接收一个promise数组,如果状态都为resolve则返回Promise的resolve状态,如果某个promise状态为reject则返回Promise的reject状态
race
接收一个promise数组, 比谁跑的快.返回第一完成的Promise状态
promise功能的简单实现
function Promise1 (executor) {
let self = this
self.status = 'pending'
self.value = undefined
self.onResolveCallbacks = []
self.onRejectedCallbacks = []
function resolve (value) {
setTimeout(() => {
self.value = value
self.status = 'resolve'
self.onResolveCallbacks.forEach(item => item(value))
})
}
function reject (value) {
setTimeout(() => {
self.value = value
self.status = 'reject'
self.onRejectedCallbacks.forEach(item => item(value))
})
}
try {
executor(resolve, reject)
} catch(e) {
reject(e)
}
}
function resoolvePromise (promise, x, resolve, reject) {
if (x === promise) {
throw new TypeError('循环引用')
}
let then
if ((x !== null)&& (typeof x === 'function' || typeof x === 'object')) {
then = x.then // x为promise
then.call(x, function (value) {
resolvePromise(promise, value, resolve, reject)
}, function (reason) {
reject(reason)
})
} else {
resolve(x)
}
}
Promise1.prototype.then = function (onFulfilled, onRejected) {
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled: function(value){ return value }
onRejected = typeof onRejected === 'function' ? onRejected: function(value){ return value }
let self = this
let promise2
if (self.status === 'resolve') {
promise2 = new Promise(function (resolve, reject) {
try {
let x = onFulfilled(self.value) // x是第二个primise运行的返回值
resoolvePromise(promise2, x, resolve, reject)
} catch(e) {
reject(e)
}
})
}
// reject
if (self.status === 'reject') {
promise2 = new Promise(function (resolve, reject) {
try {
let x = onRejected(self.value) // x是第二个primise运行的返回值
resoolvePromise(promise2, x, resolve, reject)
} catch(e) {
reject(e)
}
})
}
if (self.status === 'pending') {
promise2 = new Promise(function (resolve, reject) {
self.onResolveCallbacks.push(function () {
let x = onFulfilled(self.value) // x是第二个primise运行的返回值
resoolvePromise(promise2, x, resolve, reject)
})
self.onRejectedCallbacks.push(function () {
let x = onRejected(self.value) // x是第二个primise运行的返回值
resoolvePromise(promise2, x, resolve, reject)
})
})
}
return promise2
}
Promise1.prototype.catch = function (onRejected) {
return this.then(null, onRejected)
}
Promise1.all = function (promise) {
new Promise1(function (resolve, reject) {
let result = []
let count = 0
for (let i = 0; i < promise.length; i++) {
promise[i](function (value) {
result[i] = value
if (++count === promise.length) {
resolve(result)
}
}, function (reject) {
reject(result)
})
}
})
}
Promise1.race = function (promise) {
new Promise1(function (resolve, reject) {
for (let i = 0; i < promise.length; i++) {
promise[i](function (value) {
resolve(result)
}, function (reject) {
reject(result)
})
}
})
}
module.exports = Promise