Promise的特点:
- Promise 是一个构造函数,在创建实例时会立即执行;
- Promise 只有三个状态。fulfilled 成功,rejected 失败,pending 等待;
- Promise 的状态改变,只有两种可能:pending -> fulfilled, pending -> rejected 一旦状态确定就不可更改;
- resolve,reject函数用来更改状态。resolve :fulfilled,reject :rejected;
- then 方法内部:判断状态,如果 status === fulfilled 调用成功回调,如果 status === rejected 调用失败回调,是被定义在原型对象中的;
- then 成功回调有一个参数,表示成功后的值,then 失败回调有一个参数,表示失败的原因;
- 同一个 promise 对象下的 then 方法是可以被多次调用的;
- then 方法可以被链式调用,后面的 then 方法的回调函数拿到的值是上一个 then 方法的回调函数的返回值;
- promise 对象的返回值不能是其本身;
- all、race、resolve、finally、catch方法
部分 Promise 原理的实现:
const PENDING = "pending"
const FULFILLED = "fulfilled"
const REJECTED = "rejected"
class MyPromise {
constructor(executor) {
try {
executor(this.resolve, this.reject)
} catch (e) {
this.reject(e)
}
}
status = PENDING
value = undefined
reason = undefined
successCallback = []
failCallback = []
resolve = value => {
if (this.status !== PENDING) return
this.status = FULFILLED
this.value = value
while (this.successCallback.length) this.successCallback.shift()()
}
reject = reason => {
if (this.status !== PENDING) return
this.status = REJECTED
this.reason = reason
while (this.failCallback.length) this.failCallback.shift()()
}
then(successCallback, failCallback) {
successCallback = successCallback ? successCallback : value => value;
failCallback = failCallback ? failCallback : reason => { throw reason; }
let promise2 = new MyPromise((resolve, reject) => {
if (this.status === FULFILLED) {
setTimeout(() => {
try {
let x = successCallback(this.value)
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0)
} else if (this.status === REJECTED) {
setTimeout(() => {
try {
let x = failCallback(this.reason)
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0)
} else {
this.successCallback.push(() => {
setTimeout(() => {
try {
let x = successCallback(this.value)
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0)
})
this.failCallback.push(() => {
setTimeout(() => {
try {
let x = failCallback(this.reason)
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0)
})
}
})
return promise2;
}
catch(failCallback) {
return this.then(undefined, failCallback)
}
finally(callback) {
return this.then(value => {
return MyPromise.resolve(callback()).then(() => {
return value
})
}, reason => {
return MyPromise.resolve(callback()).then(() => {
throw reason
})
})
}
static resolve(value) {
if (value instanceof MyPromise) return value;
return new MyPromise(resolve => resolve(value))
}
static all(array) {
let result = []
let index = 0
return new MyPromise((resolve, reject) => {
const addData = (key, value) => {
result[key] = value;
index++;
if (index === array.length) {
resolve(result)
}
}
for (let i = 0; i < array.length; i++) {
let current = array[i]
if (current instanceof MyPromise) {
current.then(value => addData(i, value), reason => reject(reason))
} else {
addData(i, current)
}
}
})
}
static race(array) {
return new MyPromise((resolve, reject) => {
for (let item of array) {
if (item instanceof MyPromise) {
item.then(value => resolve(value), reason => reject(reason))
} else {
resolve(item)
}
}
})
}
}
function resolvePromise(promise, x, resolve, reject) {
if (promise === x) {
return reject(new TypeError("Chaining cycle detected for promise #<Promise>"))
}
if (x instanceof MyPromise) {
x.then(resolve, reject)
} else {
resolve(x)
}
}
module.exports = MyPromise;