promise 是一个构造函数,他的参数是一个 function,new promise()时传入的函数会立即执行
all()
race()
then()
catch()
finally()
实现
function MyPromise(fn){
function resolve(data){
console.log(data)
}
function reject(data){
console.log(data)
}
try{
fn(resolve,reject)
}catch(e){
reject(e)
}
}
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
function MyPromise(fn){
let self = this;
self.state = PENDING;
function resolve(data){
if(self.state === PENDING){
self.state = FULFILLED;
}
}
function reject(data){
if(self.state === PENDING){
self.state = REJECTED
}
}
try{
fn(resolve,reject)
}catch(e){
reject(e)
}
}
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
function MyPromise(fn){
let self = this;
self.state = PENDING;
self.value = null;
self.reason = null;
function resolve(data){
if(self.state === PENDING){
self.state = FULFILLED;
self.value = data
}
}
function reject(data){
if(self.state === PENDING){
self.state = REJECTED
self.reason = data
}
}
try{
fn(resolve,reject)
}catch(e){
reject(e)
}
}
MyPromise.prototype.then =function(onFulfilled,onRejected){
let self = this
if(self.state === FULFILLED){
onFulfilled(self.value)
}
if(self.state === REJECTED){
onRejected(self.reason)
}
}
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
function MyPromise(fn){
let self = this;
self.state = PENDING;
self.value = null;
self.reason = null;
self.onFulfilledCallback = []
self.onRejectedCallback = []
function resolve(data){
if(self.state === PENDING){
self.state = FULFILLED;
self.value = data
self.onFulfilledCallback.forEach(function(callback){
callback()
})
}
}
function reject(data){
if(self.state === PENDING){
self.state = REJECTED
self.reason = data
self.onRejectedCallback.forEach(function(callback){
callback()
})
}
}
try{
fn(resolve,reject)
}catch(e){
reject(e)
}
}
MyPromise.prototype.then =function(onFulfilled,onRejected){
let self = this
if(self.state === PENDING){//异步先加入数组,resolve或reject时执行
self.onFulfilledCallback.push(()=>{
onFulfilled(self.value)
})
self.onRejectedCallback.push(()=>{
onFulfilled(self.reason)
})
}
if(self.state === FULFILLED){//同步直接执行
onFulfilled(self.value)
}
if(self.state === REJECTED){
onFulfilled(self.reason)
}
}
MyPromise.prototype.then = function(onFulfilled,onRejected){
let self = this
let promise2 = null
promise2 = new MyPromise(function(resolve,reject){
if(self.state === PENDING){
self.OnFulfilledCallback.push(()=>{
try{
let value = onFulfilled(self.data)
self.resolvePromise(promise2,value,resolve,reject)
}catch(e){
reject(e)
}
})
self.onRejectedCallback.push(()=>{
try{
let value = onRejected(self.reason)
self.resolvePromise(promise2,value,resolve,reject)
}catch(e){
reject(e)
}
})
}
if(self.state === FULFILLED){
try{
let value = onFulfilled(self.value)
self.resolvePromise(promise2,value,resolve,reject)
}
catch(e){
reject(e)
}
}
if(self.state === REJECTED){
try{
let value = onRejected(self.reason)
self.resolve(promise2,value,resolve,reject)
}catch(e){
reject(e)
}
}
})
return promise2;
}
//onFulfilled或onRejected可能返回promise类型或基本类型值,这里统一处理
//根据PromiseA+中规范来实现[[Resolve]](promise, x)
MyPromise.prototype.resolvePromise = function(promise2,value,resolve,reject){
let self = this;
let called = false //防止多次调用
if(promise2 === value){
return reject(new TypeError('循环引用'))
}
if (x !== null && (Object.prototype.toString.call(x) === '[object Object]' || Object.prototype.toString.call(x) === '[object Function]')) {
// x是对象或者函数
try{
let then = x.then;
if(typeof then === 'function'){
then.call(value,(fulfilled)=>{
// 别人的Promise的then方法可能设置了getter等,使用called防止多次调用then方法
if (called) return ;
called = true;
slef.resolvePromise(promise2,fulfilled,resolve,reject)
},(rejected)=>{
if (called) return ;
called = true;
reject(rejected)
})
}else{
resolve(value)
}
}
catch(e){
if(called) return;
called = true;
reject(e)
}
}
else{//普通数据直接resolve
resolve(value)
}
}
MyPromise.prototype.then = function(onFulfilled,onRejected){
let self = this;
let promise2 = null;
promise2 = new MyPromise(function(resolve,reject){
if(self.state === PENDING){
self.onFulfilledCallback.push(()=>{
setTimeout(()=>{
try{
let x = onFulfilled(self.value)
self.resolvePromise(promise2,x,resolve,reject)
}catch(e){
reject(e)
}
},0)
})
self.onRejectedCallback.push(()=>{
setTimeout(()=>{
try{
let x = onRejected(self.value)
self.resolvePromise(promise2,x,resolve,reject)
}catch(e){
reject(e)
}
},0)
})
}
if(self.state === FULFILLED){
setTimeout(() => {
try{
let x = onFulfilled(self.value)
self.resolvePromise(promise2,x,resolve,reject)
}catch(e){
reject(e)
}
},0)
}
if(self.state === REJECTED){
setTimeout(() => {
try{
let x = onRejected(self.value)
self.resolvePromise(promise2,x,resolve,reject)
}catch(e){
reject(e)
} },0)
}
})
return promise2
}
MyPromise.prototype.then =function(onFulfilled,onRejected){
onFulfilled= onFulfilled !== null? onFulfilled: (data)=>{return data}
onRejected= onRejected !== null? onRejected: (data)=>{throw data}
}
MyPromise.prototype.catch = function(onRejected){
let self = this;
self.then(null,onRejected)
}
MyPromise.prototype.finally = function(fn){
return this.then((value)=>{
fn()
return value
},(reason)=>{
fn()
throw reason
})
}
done()
done方法作为Promise链式调用的最后一步,用来向全局抛出没有被Promise内部捕获的错误,并且不再返回一个Promise
MyPromise.prototype.done = function(){
this.catch((reason)=>{
throw reason
})
}
all()
MyPromise.all = function(arr){
return new MyPromise((resolve,reject)=>{
arr.forEach((promise,index)=>{
promise.then((value)=>{
result[index] = value
if(result.length === arr.length){
resolve(result)
}
},reject)
})
})
}
MyPromise.race = function(arr){
return new MyPromise((resolve,reject)=>{
arr.forEach((promise)=>{
promise.then(resolve,reject)
})
})
}
MyPromise.resolve = function (value){
let promise
promise = new MyPromise((resolve,reject)=>{
this.prototype.resolvePromise(promise,value,resolve,reject)
})
return promise
MyPromise.reject = function(value){
return new MyPromise((resolve,reject)=>{
reject(value)
})
}
参考文档